Basic Setup
IMPORTANT
This is a preliminary document for an API or technology in development. Apple is supplying this information to help you plan for the adoption of the technologies and programming interfaces described herein for use on Apple-branded products. This information is subject to change,and software implemented according to this document should be tested with final operating system software and final documentation. Newer versions of this document may be provided with future betas of the API or technology.
-
Interoperabilitylets you interface between Swift and Objective-C code,allowing you to use Swift classes in Objective-C and to take advantage of familiar Cocoa classes,patterns,and practices when writing Swift code.
-
Mix and matchallows you to create mixed-language apps containing both Swift and Objective-C files that can communicate with each other.
-
Migrationfrom existing Objective-C code to Swift is made easy with interoperability and mix and match,making it possible to replace parts of your Objective-C apps with the latest Swift features.
Setting Up Your Swift Environment
@H_403_12@ To start experimenting with accessing Cocoa frameworks in Swift,create a Swift-based app from one of the Xcode templates. @H_403_12@ To create a Swift project in Xcode-
Choose File > New > Project > (iOSorOS X) > Application >your template of choice.
-
Click the Language pop-up menu and choose Swift.
.swift
file.
@H_403_12@ From here,you can start experimenting by writing Swift code in the app delegate,or you can create a new Swift class file by choosing File > New > File > (iOSorOS X) > Source > Swift.
Understanding the Swift Import Process
@H_403_12@ After you have your Xcode project set up,you can import any framework from the Cocoa platform to start working with Objective-C from Swift. @H_403_12@ Any Objective-C framework (or C library) that’s accessible as amodulecan be imported directly into Swift. This includes all of the Objective-C system frameworks—such as Foundation,UIKit,and SpriteKit—as well as common C libraries supplied with the system. For example,to import Foundation,simply add this import statement to the top of the Swift file you’re working in:SWIFT
-
import Foundation
NSDate
,NSURL
,monospace; word-wrap:break-word; background:transparent">NSMutableData,and all of their methods,properties,and categories—directly available in Swift.
@H_403_12@ The import process is straightforward. Objective-C frameworks vend APIs in header files. In Swift,those header files are compiled down to Objective-C modules,which are then imported into Swift as Swift APIs. The importing determines how functions,classes,methods,and types declared in Objective-C code appear in Swift. For functions and methods,this process affects the types of their arguments and return values. For types,the process of importing can do the following things:
-
Remap certain Objective-C types to their equivalents in Swift,like
id
toAnyObject
-
Remap certain Objective-C core types to their alternatives in Swift,monospace; word-wrap:break-word; background:transparent">NSStringto
String
-
Remap certain Objective-C concepts to matching concepts in Swift,like pointers to optionals