Using Instances

An instance of a class (an object) has a life span: it is created, sent messages, and then destroyed when it is no longer needed.

Creating objects

To create an object, you send an alloc message to a class. In response, that class creates an object in memory and gives you a pointer to it. Creating an object looks like this:

N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​a​r​r​a​y​I​n​s​t​a​n​c​e​ ​=​ ​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​;​

Here an instance of type NSMutableArray is created, and you are returned a pointer to it in the variable arrayInstance. When you have a pointer to an instance, you can send messages to it. The first message you always send to a newly allocated instance is an initialization message. Although sending ...

Get iOS Programming: The Big Nerd Ranch Guide, 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.