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.

  1. What does NSObject provide, and when should a Swift class inherit from it? 4.3.1 · L2
    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.

  1. What is a class cluster, and what does it change for subclassing and for type checks? 4.3.2 · L3
    A class cluster is a public abstract class with several private concrete subclasses chosen by the initialiser. NSArray, NSString, NSNumber, NSData and…
  2. How does NSCopying work, and why use a copy property for strings and blocks? 4.3.3 · L2
    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