BlogiOS
Moving Objective-C to Swift, one file at a time
A practical way to modernise an older iOS codebase without stopping feature work.
Plenty of successful iOS apps still contain a lot of Objective-C. It works, but it slows teams down: new developers don’t know it, and modern frameworks like SwiftUI expect Swift. Part of my work on the Colect and Le New Black iPad apps is exactly this kind of modernisation, so here’s how I approach it.
Don’t rewrite. Migrate as you go.
Swift and Objective-C live happily in the same target, so there’s no need for a big-bang rewrite. Two rules keep the migration moving: all new code is written in Swift, and when you change an Objective-C file for a feature or a bug, you consider converting it in the same pull request. The codebase gets more Swift every sprint, and feature work never stops.
Make Objective-C friendlier to Swift first
Before converting anything, annotate your Objective-C headers. It’s cheap and immediately improves every Swift file that calls them:
NS_ASSUME_NONNULL_BEGIN
@interface ProductStore : NSObject
@property (nonatomic, copy) NSArray<Product *> *products;
- (nullable Product *)productWithID:(NSString *)productID
NS_SWIFT_NAME(product(id:));
@end
NS_ASSUME_NONNULL_END
Swift now sees products as [Product] instead of [Any], and product(id:) returns Product? instead of an implicitly unwrapped optional. Nullability annotations, lightweight generics, NS_SWIFT_NAME and NS_ENUM do most of the work.
Know how the bridge works
Swift calls Objective-C through the bridging header. Objective-C calls Swift through the generated YourModule-Swift.h header, which only contains what Swift exposes with @objc. That second direction has limits: Swift structs, enums with associated values, generics and most protocol extensions aren’t visible to Objective-C. That shapes the order of the work.
Convert from the outside in
Swift calling Objective-C is smooth. Objective-C calling Swift forces you into @objc-compatible types. So start with code that nothing else in Objective-C depends on: new features first, then screens and view controllers, then the services they use. Leave shared models and core utilities for last, when few Objective-C callers remain. By then you can turn them into proper Swift types, like structs and enums, instead of NSObject subclasses kept only for compatibility.
If an Objective-C class needs to call a Swift one early, add a thin @objc wrapper and delete it later.
Lock behaviour with tests before you convert
A conversion should change the language, not the behaviour. Before converting a class, write XCTest cases around its public methods, run them, convert the class and run them again. Even a handful of tests catches most mistakes.
Watch for the subtle differences
- Messaging nil. In Objective-C, calling a method on
nilquietly returns nil or zero. In Swift, optionals make you handle it. Converted code sometimes reveals bugs that were there all along. - Numbers.
NSNumber,NSIntegerandCGFloatmap to Swift types, but mixed arithmetic needs explicit conversions. - Dynamic features. KVO needs properties marked
@objc dynamic, and selectors need@objcmethods. - Singletons and global state. A conversion is a good moment to inject dependencies instead of calling shared instances everywhere.
Then bring in SwiftUI
Once a flow is in Swift, adding SwiftUI screens becomes easy. I wrote about that next step in Adding SwiftUI to a UIKit app without a rewrite.
Make progress visible
Track the share of Swift files over time, for example with a small script in CI that counts .swift and .m files. It keeps the migration on everyone’s radar and makes it easy to show the team, and the business, that it’s moving.
Nishanth Kumar