Important: This article provides an initial introduction to the API or technology used in development. Apple provides this information to help you plan the technologies and interfaces described in this article for use in Apple products. This information is subject to change, and the software implemented according to this article should be tested on the latest operating system and against the latest documentation. New versions of this document may be available in the future via seeds versions of the technology and API
Swift was designed to work seamlessly with Cocoa and Objective-C. In Swift, you can use the Objective-C API (including the system framework and your custom code), and you can use the Swift API in Objective-C. This compatibility makes Swift a simple, convenient, and powerful tool to integrate into your Cocoa application development workflow.
This guide includes three important aspects of compatibility that make it easier for you to develop Cocoa applications:
- Interoperability allows you to interface Swift with Objective-C, allowing you to use Swift classes in Objective-C and leverage familiar Cocoa classes, patterns, and practices when writing Swift code.
- Mix and match allows you to create mixed language applications that combine Swift and Objective-C files to communicate more with each other.
- Migration Due to the above two points, migrating from existing Objective-C code to Swift is very simple, making it possible to replace parts of your Objective-C application with the latest Swift features.
Before you start learning about these features, you need a general understanding of how to set up a Swift environment to access the Cocoa system framework.
As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public number: Programming Daxin, whether you are small white or big ox are welcome to enter. Let us progress together and develop together! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)
Set up your Swift environment
To start experimenting with accessing the Cocoa framework in Swift, use one of Xcode’s templates to create a Swift-based application.
Create a Swift project in Xcode
-
Choose File > New > Project > (iOS or OS X) > Application > Your Template of Choice.
-
Click on the Language pop-up menu and select Swift.
The structure of a Swift project is almost identical to an Objective-C project, with one important difference: Swift has no header files. There is no explicit division between the implementation and the interface, so all information in a particular class is stored in a separate. Swift file.
To get started, you can start experimenting with writing Swift code in an App Delegate, or you can create a Swift class by selecting File > New > File > (iOS or OS X) > Other > Swift.
Understand the Swift import process
After you set up your Xcode project, you can import into Swift any Cocoa platform framework that works in Objective-C.
Any Objective-C framework (or C class library) will act as a module that can be imported directly into Swift. These include all objective-C system frameworks — such as Foundation, UIKit, and SpriteKit — as well as common C class libraries supported by the system. For example, to import Foundation, simply add an import statement to the top of your Swift file.
SWIFT import Foundation
This import imports all of the Foundation apis, including NSDate, NSURL, and NSMutableData, and all of their methods, attributes, and categories can be used directly in Swift.
The import process is very neat. The Objective-C framework declares the API in a header file. In Swift, those header files are compiled into objective-C modules, which are then imported into Swift as the Swift API. Imports determine how objective-C functions, classes, methods, and types appear in Swift. For Function and Method, this procedure affects their arguments and return values. The import process can do the following:
- Remap identified Objective-C types to the equivalent type in Swift, just like ID to AnyObject
- Remap identified Objective-C core types to alternative types in Swift, just like NSString to String
- Remap identified Objective-C concepts to matching concepts in Swift, such as Pointers to Optionals
In the interoperability section, you’ll learn more about how these mappings work in your Swift code. Importing Swift’s model into Objective-C is very similar to importing Swift from Objective-C. Swift declares its API, such as a framework for Swift Modules. Also these Swift modules generate objective-C header files. These header files can be mapped back to the Objective-C API. Some of Swift’s apis don’t map back to Objective-C because they’ve chosen the language features and found that they’re not available in Objective-C.
Note: you cannot import C++ code directly into Swift. The solution is to create an Objective-C or C wrapper for C++ code.