iPhone Development 101

iPhone 101

Code Tips

Resources

Links

Subscribe

TwitterRSSE-mail

More

Learn iPhone Programming:
Intro to Objective-C: Instances

This tutorial was written for Xcode 3.2; it's outdated now. For a more up-to-date tutorial, check out Apple's App Development Tutorial to learn how to write iOS apps. There are also lots of other excellent books and online tutorials you can learn from.

I'll still be posting code tips and shortcuts to idev101; click here for the index. If you'd like to get updates, follow me on Twitter, or by RSS or E-mail.

What's the difference between a class and an instance?

A class contains the instructions for how objects behave. The instances are the objects themselves. It's a bit like the difference between a blueprint and a house... or a recipe and the cookies. The class is the recipe. The instances are the cookies.

In the @interface file you created declarations to instance variables. A declaration tells the compiler about the variable; it doesn't allocate memory or create the actual instance. An instance is created with the following code:

ClassName *instanceName = [[ClassName alloc] init];

This allocates memory for the object (with alloc), initializes it (by calling the init method on the class), and assigns it to instanceName.

The instance can now set properties and call any instance methods defined by its parent class. (Or any of its parent's parents. In an object-oriented language, an instance inherits all of the public properties and methods of all of its parent classes, all the way up to the root class NSObject.)

You can also create instances of UI objects (buttons, text fields, images) in Interface Builder; we'll be looking at that soon.

Releasing Instances

When you use alloc/init to create an object, you're reserving memory for that object. When you're done using the object, you must release it to free up that memory:

[instanceName release];

Objective-C uses retain counts to keep track of the number of pointers to an object. When you alloc an object and assign it to an instance variable, the object's retain count is 1. If another object retains your object, the retain count is increased to 2. Releasing an object decreases the retain count by 1. When the retain count gets to 0, the object is destroyed and the memory space it was using is freed.

Here's a more in-depth discussion of Memory Management in Objective-C.

Releasing Properties

You must release any @property variables that used copy or retain; these are released in the dealloc method:

- (void)dealloc {
    [window release];   // releases the window object
    [super dealloc];
}

Autorelease

Sometimes you'll need to create an object without knowing when it will be released. For example, if you're returning an object from a method, obviously you can't release the object before returning, or there'll be nothing to return.

The solution here is to use autorelease. When an object is sent the autorelease message, it's added to the program's AutoRelease Pool. Periodically the pool is "drained" and all objects in the pool are sent a release message. If the object is no longer in use, its memory space is freed automatically.

Many Cocoa classes have methods that return autoreleased values.

// create an autoreleased instance of ClassName:

ClassName *instanceName = [[[ClassName alloc] init] autorelease];

// or:

ClassName *instanceName = [[ClassName alloc] init];
[instanceName autorelease];

// a class method that returns an autoreleased value:

NSString *myString = [NSString stringWithString:@"this is a test."];


TopHomeContents