Book Image

Learning Swift

By : Andrew J Wagner
Book Image

Learning Swift

By: Andrew J Wagner

Overview of this book

Table of Contents (18 chapters)
Learning Swift
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Calling Objective-C code from Swift


The last and possibly the most critical component of understanding Objective-C for our purpose is the ability to call Objective-C code from Swift. This is actually pretty straightforward in most circumstances. We will not take any time to discuss calling Swift code from Objective-C because we assume that you are only writing Swift code.

A bridging header

The most important part of being able to call Objective-C code from Swift is understanding how to make the code visible to Swift. As we know now, Objective-C code needs to be imported to be visible to other code. This still holds true with Swift, but Swift has no mechanism to import individual files. Instead, when you add your first Objective-C code to a Swift project, Xcode asks you whether you want to add what is called a bridging header:

You should select Yes, and then Xcode automatically creates a header file named after the project ending in Bridging-Header.h. This is the file where you need to import...