For an app with a decade of Objective-C screens, a C image decoder and Objective-C++ wrappers around a C++ geometry library, how would you plan the Swift migration of its interop boundaries?
answer
- three boundaries, three tools
- annotate headers before porting
- who still subclasses the class
- implement the header in Swift
- where the C++ mode stops
basics
~20 sAnnotate the Objective-C and C headers first, write new leaf code as @objc Swift, port classes Objective-C still subclasses with @objc @implementation (Swift 6.1), and replace Objective-C++ wrappers with C++ interoperability only behind one Swift module that keeps the mode contained.
solid answer
~40 sTreat each boundary separately. First make the headers honest: nullability audit regions, lightweight generics, `NS_ENUM`/`NS_OPTIONS`, `NS_SWIFT_NAME` and Core Foundation ownership annotations, because every Swift caller improves at once. Write new screens and services in Swift and expose only what Objective-C still calls with `@objc`. For an existing class that Objective-C code subclasses or swizzles, an `@objc` Swift class is the wrong tool; since Swift 6.1 an `@objc @implementation` extension implements the existing header in Swift, one category at a time, and the class stays a real Objective-C class. For the C++ library, replace the Objective-C++ wrappers with C++ interoperability when its API imports cleanly, and contain the mode in one Swift module; keep the wrappers where Objective-C callers remain or the C++ API needs too many annotations. Shrink the `@objc` surface as callers disappear.
code
swift · 23 lines// GalleryController.h stays the Objective-C source of truth:
// NS_ASSUME_NONNULL_BEGIN
// @interface GalleryController : NSObject
// @property (nonatomic, copy) NSString *title;
// - (instancetype)initWithTitle:(NSString *)title;
// - (void)reloadPhotos;
// @end
// NS_ASSUME_NONNULL_END
import Foundation
@objc @implementation extension GalleryController {
var title: String
final var cache: [String: Int] = [:] // Swift-only helper storage
init(title: String) {
self.title = title
super.init()
}
func reloadPhotos() {
cache.removeAll()
}
}go deeper
Recall the pieces: bridging header and generated header for Objective-C, header annotations for C, and C++ interoperability mode for C++.
Explain why header annotations come first and what @objc exposure costs, so a migration step improves Swift callers without breaking Objective-C ones.
Show when @objc @implementation is required instead of an @objc class, and how to contain the C++ mode behind one Swift module.
Own the sequencing and the boundary shapes: which classes move first, which wrappers stay, how progress is measured, and what toolchain churn the C++ choice commits the team to.
## Three boundaries, three toolkits A decade-old app rarely has one interop problem. It has several, and each has its own Swift tools: | Boundary | Today | Swift-side tools | |---|---|---| | Objective-C screens and models | `.h`/`.m` pairs, some subclassed | bridging header imports; `@objc` Swift classes; `@objc @implementation` extensions (Swift 6.1) | | C image decoder | plain C header, CF return values | header annotations (nullability, `CF_RETURNS_RETAINED`); a small Swift wrapper module | | C++ geometry library | Objective-C++ `.mm` wrappers | C++ interoperability mode (Swift 5.9); `SWIFT_SHARED_REFERENCE` and ownership annotations | The decision a lead owns is the **order** and the **shape of each boundary**, not whether to rewrite everything. ## Step one: make the headers honest Before porting any implementation, improve how the existing headers import: - wrap headers in `NS_ASSUME_NONNULL_BEGIN`/`END` (or `NS_HEADER_AUDIT_BEGIN(nullability, sendability)`) and mark the true `nullable` pointers; - parameterize collections (`NSArray<NSString *> *`); - turn integer `typedef`s into `NS_ENUM`, `NS_CLOSED_ENUM` or `NS_OPTIONS`; - rename awkward imports with `NS_SWIFT_NAME`; - annotate the C decoder's Create/Copy functions with `CF_RETURNS_RETAINED` so Swift never sees `Unmanaged`. This is cheap, it changes no Objective-C behaviour, and every later Swift file benefits. ## Moving Objective-C classes: two different mechanisms **`@objc` Swift classes** are right for new leaves: view models, services, new screens. Objective-C reaches them through the generated header. Their limits matter for old code: the generated header cannot be included in the module's own headers, and an `@objc` class keeps Swift vtables and other Swift-specific data, so Objective-C code cannot subclass it or reliably swizzle its methods. **`@objc @implementation` extensions**, implemented in Swift 6.1 (SE-0436), invert the direction. The Objective-C header stays the source of truth; a Swift extension replaces the `@implementation` block: 1. Swift checks that every member declared in the `@interface` has a matching implementation with the same Swift name and selector, and rejects an extension that leaves one out. 2. The class gets Objective-C class metadata without the Swift-specific parts, so existing Objective-C subclasses keep working. 3. You can port one category at a time with `@objc(CategoryName) @implementation extension`. 4. `final` members are Swift-only helpers and may use Swift-only types. It does not fit everything: the class must be a non-root class without lightweight generics, and a class whose stored-property layout cannot be computed at compile time needs newer runtime support. Classes that are only called, never subclassed, can still move to plain Swift. ## The C++ decision: replace the Objective-C++ wrappers or keep them C++ interoperability lets Swift call the geometry library directly, but it is a build-wide commitment: - **For replacing**: the wrappers are boilerplate that duplicates the API, value types and `std::vector` import well, and reference-counted classes can be annotated `SWIFT_SHARED_REFERENCE`. - **For keeping**: Objective-C screens still call the wrappers; the C++ API relies on constructs that import as unsafe or need many annotations; and every module that sees C++ types through a public import must enable the mode. - **The middle path**: one Swift `GeometryKit` module imports the C++ library with `internal import`, exposes Swift types only, and replaces the wrappers for Swift callers, while the `.mm` wrappers shrink to what Objective-C still needs. C++ interoperability is still evolving between releases, so pin the compatibility version and budget for toolchain upgrades. ## Risks to manage - **Binary size and launch cost**: every `@objc` entry point needs a thunk and metadata; SE-0160 reports that preliminary tests on some apps found 6-8% of binary size in such thunks, so blanket `@objcMembers` during a migration is not free. - **Silent behaviour changes**: an Objective-C caller that reaches Swift through a string selector or KVO fails at run time rather than at compile time when exposure is narrowed. - **Toolchain coupling**: `@objc @implementation` needs Swift 6.1 or later, and with C++ interoperability the Swift toolchain's own Clang parses the C++ library's headers, so header changes and toolchain upgrades move together. ## Sequencing and measuring 1. Annotate headers and fix the warnings they expose. 2. Build new features in Swift with narrow `@objc` exposure. 3. Port leaf Objective-C classes; use `@objc @implementation` where Objective-C subclasses or swizzles them. 4. Introduce the Swift geometry module and retire wrappers caller by caller. 5. Remove `@objc` members once no Objective-C code calls them. Track progress with the generated header's size, the count of `@objc` declarations, and the number of `.m` and `.mm` files. Swift 6 mode adds its own work: the compiler inserts dynamic isolation checks in `@objc` thunks of synchronous actor-isolated members, which the concurrency material covers.
- An Objective-C class has a dozen Objective-C subclasses across old screens; can you port it as an @objc Swift class?Not safely. An `@objc` Swift class still carries Swift vtables and metadata that the Objective-C compiler and runtime do not fully understand, so Objective-C code cannot subclass it or reliably swizzle its methods. Keep the header and implement it with an `@objc @implementation` extension (Swift 6.1 and later), which emits a genuine Objective-C class, so the existing subclasses keep working.
- When would you keep the Objective-C++ wrappers even after adopting C++ interoperability?While Objective-C screens still call them, when the C++ API leans on constructs that import as unsafe or need many annotations, or when enabling the mode in every module that would see C++ types is not acceptable. Then the wrappers remain the stable boundary, and only new Swift code reaches the library through a contained Swift module.
- Rewrite every Objective-C class in Swift before annotating any headers.
- An @objc Swift class can replace any Objective-C class, even one Objective-C subclasses.
- @objc @implementation lets you implement only some members of an @interface.
- Turning on C++ interoperability in one target never affects modules that import it.
- Objective-C++ wrappers should all be deleted the day C++ interoperability is enabled.
Assumes Swift 6.4: @objc @implementation shipped in Swift 6.1 (SE-0436) and C++ interoperability in Swift 5.9.