4.1 · Message send, selectors, forwarding, isa
Objective-C does not call methods, it sends messages. Everything dynamic about the platform, from KVO to swizzling to target-action, is a consequence of that one design.
Mental model. A method call in Objective-C is a question asked at runtime: “does this object respond to this selector?” Swift can ask it too, but only when you opt in.
Messages and selectors
How a send is looked up, named and answered.
- What happens when you send a message in Objective-C, and what if nothing responds?
[obj doThing:x] compiles to a call to objc_msgSend(obj, @selector(doThing:), x). The runtime reads the object’s class pointer (isa), looks the selector up in… - What is a selector, how does #selector map Swift names to it, and what does the colon count mean?
A selector (SEL) is the runtime’s name for a message: an interned string such as insertString:atIndex:. Its colons give the argument count, and the argument…
When the answer is “no”
Forwarding, and the runtime class that hides behind your object.
- What are the stages of message forwarding, and when would you use them?
When a selector is not found, the runtime gives the object several chances before it throws. First, resolveInstanceMethod: may add the method dynamically (how… - What is isa, and why does your object’s runtime class sometimes differ from its Swift type?
Every Objective-C object starts with a pointer, isa, to its class object, which holds the method table, the superclass pointer and the instance layout. The…
More in Objective-C & interop
- 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.9 · How Objective-C declarations arrive in Swift
- 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