4.9 · How Objective-C declarations arrive in Swift
Swift does not see your header as written. The importer applies mechanical rules, and knowing them tells you what a call will look like, what the compiler will insist on, and which annotation fixes an awkward result.
Mental model. The importer is a translator with a phrasebook. If you know its rules you can predict the Swift spelling from the header, and you can write headers that translate well.
Types and construction
Enums, option sets, initializers and the rules that name them.
- How do NS_ENUM, NS_OPTIONS and NS_CLOSED_ENUM import, and why does Swift demand @unknown default?
NS_ENUM(NSInteger, Mode) imports as a Swift enum with the common prefix removed (ModeOn becomes .on) and a rawValue of the declared integer type. NS_OPTIONS… - How do Objective-C initializers and factory methods import, and what do designated initializers and NS_UNAVAILABLE change?
An -initWithOwner: method imports as init(owner:): the initWith prefix is dropped and the first label comes from the rest of the selector. instancetype imports… - What does atomic actually guarantee for an Objective-C property, and how does Swift see nonatomic and atomic?
Objective-C properties are atomic by default. For an object property that means the synthesized getter and setter take a lock (or an equivalent) so a reader… - What are NSNotFound and the other sentinel values, and how do they show up in Swift?
Foundation predates optionals, so “not found” was written as a magic number. NSNotFound is NSIntegerMax, which is Int.max on 64-bit. APIs such as…
Blocks, pointers and callbacks
Out parameters, escaping rules, optional delegate methods and completion handlers.
- How do pointer and out-parameter conventions such as BOOL *stop import into Swift?
Objective-C passes extra results through pointers, and Swift maps each shape to a type. NSError ** becomes throws (see the error question). A BOOL * “stop”… - Are Objective-C block parameters escaping in Swift, and what does NS_NOESCAPE change?
The importer cannot know what the Objective-C method does with a block, so it assumes the safe-for-the-callee answer: an ordinary block parameter imports as an… - How do @optional protocol methods work, and how do you write a delegate in Swift that implements only some of them?
Objective-C protocols can mark methods @optional, and the caller checks before sending: [delegate respondsToSelector:@selector(didProgress:)]. Swift has no… - How does an Objective-C completion-handler method become an async function in Swift, and how do you control it?
The importer recognises a method whose last parameter is a block returning void and which is named like a completion handler (completion, completionHandler…
More in Objective-C & interop
- 4.1 · Message send, selectors, forwarding, isa
- 4.2 · Categories, associated objects, load and initialize
- 4.3 · NSObject, class clusters, copying
- 4.4 · @objc, what does not bridge, and error bridging
- 4.5 · Nullability, lightweight generics, Swift-friendly names
- 4.6 · Method swizzling, and when it is a footgun
- 4.7 · Reading Objective-C, blocks, and manual reference counting
- 4.8 · Mixed-language projects, costs, and migrating a large app
- 4.10 · The runtime up close: introspection, encodings, IMPs and exceptions
- 4.11 · Key-value coding and observing
- 4.12 · What crosses the boundary: inference and concurrency