3. Memory Management

If you come from a C or C++ background, you’re probably used to tracking ownership of objects and manually allocating and destroying them. If you’re coming from a language such as Java, you’re probably accustomed to having the garbage collector take care of all of this for you.

Objective-C does not, at the language level, provide anything for allocating or deallocating objects. This is left up to C code. You commonly allocate objects by sending their class a +alloc message. This then calls something like malloc() to allocate the space for the object. Sending a -dealloc message to the instance will then clean up its instance variables and delete it.

The Foundation framework adds reference counting to this simple manual memory ...

Get Objective-C Phrase Book, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.