One of the common questions I am hearing, and a question I myself have is, “How can I use Swift with my existing Objective-C-based iOS applications?”
It’s obvious this question has been on Apple’s mind for quite some time, and they’ve certainly put a lot of thought into it. You can find iOS Developer Library resources on using Swift with Cocoa and Objective-C and for each of the following topics:
With these resources you can use Swift with your existing Objective-C code or migrate your Objective-C code over to Swift.
If you choose to take advantage of interoperability, one route you might take is to interact with existing Objective-C APIs.
You can interface between Swift and Objective-C in either direction. If you use existing Objective-C APIs or you’ve created your own Objective-C APIs for your applications, you can import that Objective-C framework, instantiate classes from it, and interact with them natively using Swift.
Another route you might take is to write Swift Classes with Objective-C Behavior. You can subclass Objective-C classes and adopt Objective-C protocols to take advantage of the familiar functionality of Objective-C and enhance them with Swift’s more modern language features.
Swift also provides easy ways to work with existing Cocoa data types. Swift performs automatic conversion on some Objective-C types to Swift types, and some Swift types to Objective-C types. You can also use a number of data types in Swift and Objective-C that are interchangeable. Data types that can be used interchangeably are referred to as bridged data types.
You can also adopt Cocoa’s established design patterns in your Swift applications. Because Swift works well with Objective-C, you can use common Cocoa design patterns in your Swift code, and you can use Swift modern language features to extend or simplify these patterns.
Finally, as part of its interoperability with Objective-C, Swift is compatible with many C language types and features.
Mixing and Matching Objective-C and Swift
Because of Swift’s interoperability with Objective-C, you can create projects that contain files written in either language. The ability to mix and match and write apps with a mixed-language codebase is a “feature” of Swift.
Migrating Objective-C Code to Swift
At this point you can use mix and match plus interoperability to start migrating your Objective-C codebase to Swift. You can use these features to integrate Swift into existing Objective-C apps without having to rewrite the entire app in Swift all at once. When you are ready to migrate your existing Objective-C apps you will want to make sure you’ve achieved optimal compatibility between your Objective-C and Swift code. Once ready, Apple has put together a nice migration process for you to follow.
Use the information provided above and links to valuable iOS Developer Library resources to start using Swift with your existing Cocoa and Objective-C codebases. If there is one thing I’ve learned about Apple over the years it’s that they put a lot of thought and work into making their products great for users. Swift is another one their products and the extent of the documentation they’ve provided is just another attribute to their company’s focus on the user.
Thanks for reading! I hope this article has helped you answer the questions you have about using Swift with your existing Objective-C apps. Please let me know if you have any questions in the comments.