4.7 · Reading Objective-C, blocks, and manual reference counting

Most mixed codebases still contain files written before ARC and before Swift. You do not have to love them, but you do have to read them quickly and spot the traps.

Mental model. Old Objective-C is Swift with square brackets and fewer safety nets. The same ideas apply; the compiler just checks less.

Reading the language

The syntax and the habits that look strange to a Swift reader.

  1. How do you read Objective-C if you only know Swift, and what looks different but means the same? 4.7.1 · L2
    The shapes map one to one. A method call [obj doThing:x with:y] is obj.doThing(x, with: y). @interface declares the public API (in the .h), @implementation…

Blocks and manual counting

Closures the old way, and memory management before ARC.

  1. How do Objective-C blocks differ from Swift closures, especially for capture? 4.7.2 · L2
    A block (^{ … }) is Objective-C’s closure, and it imports into Swift as a closure with @convention(block). The key difference is capture semantics. A block…
  2. What do you need to know to read and safely touch manual reference counting code? 4.7.3 · L3
    In a file compiled with -fno-objc-arc you will see retain, release and autorelease written out, and dealloc ending with [super dealloc] (forbidden under ARC).…

More in Objective-C & interop