3. Two-Stage Creation

Cocoa relies on conventions established by the NSObject base class to allocate and initialize new instances of Cocoa classes. The reliance on conventions for something as central to an object-oriented language as instance creation may seem problematic at first, but it works well in practice. Several interrelated patterns are used to assure correct allocation and initialization of instances.

Many languages, such as Java, C++, Ruby, and Smalltalk use a method named “new” to allocate and initialize new instances. Even though NSObject implements a +new method, Cocoa developers seldom, if ever, use it. The Two-Stage Creation pattern separates the first stage, object memory allocation, from the second stage, object initialization. ...

Get Cocoa Design Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.