4.3 · NSObject, class clusters, copying
The root class of Cocoa, the factory pattern behind its collections, and the copying rules that decide whether you share or duplicate.
Mental model. Most Cocoa types are really one public name with several private classes behind it. Knowing that explains subclassing limits, odd class names and why copy is sometimes free.
The root class
What NSObject gives you, and when a Swift class should adopt it.
- What does NSObject provide, and when should a Swift class inherit from it?
NSObject is the root of the Cocoa hierarchy. It supplies the runtime interface: isEqual: and hash, description, introspection (isKind(of:), responds(to:)…
Class clusters and copying
Hidden subclasses, and copy semantics for immutable and mutable types.
- What is a class cluster, and what does it change for subclassing and for type checks?
A class cluster is a public abstract class with several private concrete subclasses chosen by the initialiser. NSArray, NSString, NSNumber, NSData and… - How does NSCopying work, and why use a copy property for strings and blocks?
NSCopying gives copy, NSMutableCopying gives mutableCopy. For an immutable object a conforming class may return itself, retained, from copy: copying NSArray or…
More in Objective-C & interop
- 4.1 · Message send, selectors, forwarding, isa
- 4.2 · Categories, associated objects, load and initialize
- 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