Should I Convert Everything to Swift?
Once you’ve made the decision to move to a Swift code base, you don’t have to do it all at once. In fact, it’s perfectly fine to leave some parts of your app in Objective-C.
Think of the migration as a marathon, not a sprint. You want to make sure you do it right. You want to make sure that you finish it with a healthy code base.
Small, Focused Changes
As with most challenges in software development, it’s much easier to take it one step at a time. Converting an entire code base at once can leave a lot on your hands to deal with. Rather do your conversion in small, focused increments. Make a change, test that the project still works, and then repeat.
What Should I Leave in Objective-C?
You won’t be able to convert your third-party Objective-C libraries to Swift, and there’s nothing wrong with that. Xcode offers great interoperability between Objective-C and Swift.
The Foundation and UIKit frameworks are still written in Objective-C. As such, Apple designed the Swift runtime to work extremely well with those libraries.
When writing your app logic in Swift, you won’t have to worry about what language your dependencies choose to use.
Some Objective-C libraries have a native Swift replacement (i.e. AFNetworking → Alamofire). While it’s a great idea to move to Swift libraries long- term, even libraries like AFNetworking can be still used from Swift, so don’t feel obligated to.
Helper and Extension Classes
Some classes in your code base might be used in many different places across your app. This makes the conversion tricky. As soon as you convert those classes, you’ll have to update everywhere that they’re used. This goes against the idea of small, focused changes.
Rather leave these classes as Objective-C for now. You can update it to Swift later on once most of the other classes have been through the conversion.
Uses of the Objective-C Runtime
Certain aspects of the Objective-C runtime, such as method swizzling or making use of
objc_msgSend, can only be used from Objective-C.
Either leave this code in Objective-C or rewrite it to solve your problem using functionality available in Swift.
Low-level code has usually been specifically fine-tuned for performance. You will want to give these critical parts of your app more attention and testing before attempting any conversion.
Anything that uses libraries depending on C/C++ code will have to be left in Objective-C. For example, a popular computer vision library, OpenCV, is written in C++. If you make use of the OpenCV library in your app, the code that uses OpenCV should stay as Objective-C.
Your Core Data related code might also need some consideration, so this may go lower on your priority list.
Additionally, there are parts of the Cocoa API that Swift can’t access. For example, you can’t form a C function or a pointer-to-function in Swift.
Where To Go From Here?
Using what you’ve learned from this post, you can begin the process of strategically migrating your Objective-C project to Swift.
Swiftify’s new Advanced Project Converter streamlines the process of converting Objective-C to Swift. Use it if you’d like a tool to make the process even simpler.
Have you struggled to convert any other parts of your code base to Swift? If you have, let us know in the comments below.