01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating and destroying objects<br />

115<br />

chapter 8. For now, let’s move on and discuss how you can create new instances of the<br />

CTRentalProperty class.<br />

5.5 Creating and destroying objects<br />

One of the most common tasks to perform with classes is the creation of new objects.<br />

In <strong>Objective</strong>-C it takes two steps to create an object; in order, you must<br />

■<br />

■<br />

Allocate memory to store the new object.<br />

Initialize the newly allocated memory to appropriate values.<br />

An object isn’t fully functional until both steps are completed.<br />

5.5.1 Creating and initializing objects<br />

For objects that inherit from NSObject, memory for new objects is typically allocated<br />

by calling the class method alloc (short for allocate). As an example, you could create<br />

a new CTRentalProperty object by executing the following line of source code:<br />

CTRentalProperty *newRental = [CTRentalProperty alloc];<br />

This statement uses the alloc method to reserve enough memory to store all the<br />

instance variables associated with a CTRentalProperty object and assigns it to a variable<br />

named newRental. You don’t need to write your own implementation of the<br />

alloc method because the default version inherited from NSObject is suitable. Most<br />

objects, however, do require additional initialization once the memory has been allocated.<br />

By convention, this initialization is achieved by calling a method named init:<br />

CTRentalProperty *newRental = [CTRentalProperty alloc];<br />

[newRental init];<br />

It’s even possible to perform both of these steps in a single line by nesting the message<br />

sends as follows:<br />

CTRentalProperty *newRental = [[CTRentalProperty alloc] init];<br />

This works because the init method conveniently returns the object it’s called upon<br />

(self) to support such usage.<br />

What do uninitialized instance variables get set to?<br />

You may wonder what value your instance variables will have if your initialization<br />

method doesn’t explicitly initialize them. Unlike typical C-style memory allocation<br />

strategies, the memory returned by alloc has each instance variable initialized to<br />

the value zero (or its equivalent: nil, NULL, NO, 0.0, and so on). This means you<br />

don’t need to redundantly initialize variables to such values.<br />

In fact, you should consider choosing meanings for Boolean variables such that NO<br />

is the appropriate state of object allocation. For example, isConfigured may be a<br />

better instance variable name than needsConfiguration.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!