Cocoa Programming/Objective-C basics

From Wikibooks, open books for an open world
Jump to: navigation, search
Cocoa Programming
First Steps | Objective-C basics | Memory Management | Foundation Kit | Application Kit | Web Kit

Objective-C is the native language of Cocoa applications. It is an object-oriented superset of ANSI C, allowing the developer to include "plain C" statements and code into the program. Any ANSI C program is a valid Objective-C program.

NSObject[edit]

The Objective-C used in Cocoa is a bit different from "plain" Objective-C, because of the Cocoa runtime system which defines a new root class, NSObject. All Cocoa objects should be descendants of NSObject in order to gain from its improved memory management model.

NSObject is defined in the header <Foundation/NSObject.h>.

The NSObject life cycle[edit]

To create an NSObject, send its class either an alloc or a new message:

id anObject = [NSObject new];  // Creates and returns an initialized object
 
id anotherObj = [NSObject alloc];   // Not yet ready for use...
anotherObj = [anotherObj init];     // ...now it is.
 
// good style: combine alloc and init:
id oneMoreObj = [[NSObject alloc] init];  // identical to using new

Although "new" and "alloc followed by init" methods appear to be identical, Apple documentation seems to give preference to the latter method. This gives developers the ability to override alloc or init separately to achieve a certain result, if necessary.

Note that initializing an object may sometimes fail: for example a file containing the initialization data may be unavailable. In that case, init will return nil. Therefore, it is always necessary to check the return value of init before using the object!

After an object has been initialized, it is ready for use. To use an object, send it messages:

[anObject aMessage];

If the object understands the message, it will react to it. Otherwise, a trap is generated.

When an object is no longer used, it must be released. To release an object, send it a release message.

[anObject release];

This will lead to the destruction of the object (unless it's still in use somewhere else, see Cocoa memory management for details).

When an object is about to be destroyed, it will receive a dealloc message. If the object has reserved any resources, it should release them now.

Extending NSObject[edit]

All classes in an Cocoa/Objective-C application should be derivatives of NSObject. Thus, when defining a new class you should follow some basic rules:

  • Derive from NSObject:
  • Either directly or from a derivative
  • Define initializer & deallocator
  • Call base class initializer & deallocator

Deriving from NSObject[edit]

To create your class as a subclass of NSObject, define it as follows (in the interface definition file "MyClass.h"):

#import <Foundation/NSObject.h>
 
@interface MyClass : NSObject
{
@private
  /* instance variable declarations */
  AnotherClass* myMember;
}
/* method declarations */
@end

Defining initializer and deallocator[edit]

An initializer is a method that initializes an object. In Cocoa, the initializer for NSObject is called init. The init method for NSObject does not know about user-defined classes and cannot therefore initialize their instances properly. Thus, it is essential to write an initializer method for your class. If the initialization does not require any parameters, the method to define is called init (i.e., you should override the base class initializer). This makes it possible to create instances of your class by using the new class method derived from NSObject:

MyClass* myObj = [[MyClass alloc] init];   // allocate and initialize the object

The above code creates a new instance of MyClass. Since MyClass has no methods called alloc or init, the messages are automatically sent to its base class.

To override the initializer for your class, optionally define it in the interface file:

/* In MyObject.h, in the method declarations part */
- (id) init;

and implement it in the .m file:

/* In MyObject.m */
@implementation MyObject
- (id) init
{
    /* first initialize the base class */
    self = [super init]; 
    /* then initialize the instance variables */
    myMember = [AnotherClass new];
    /* finally return the object */
    return self;
}
@end

If the initialization requires additional parameters, you should define an initializer like initWithParam:, for example

- (id) initWithMember: (AnotherClass) member
{
    self = [super init];
    myMember = [[AnotherClass alloc] initWithValue: member];
    return self;
}

Even then, it is often advisable to define a "default" initializer that calls your initWithXXX method with suitable default parameters:

- (id) init
{
    return [self initWithMember: someDefaultValue];
}

A deallocator is a method that is responsible to release all resources an object holds. The deallocator is called before an object is released, thus making sure everything gets cleaned up properly. In Cocoa, the deallocator method is dealloc. Every class that holds any resources should define a dealloc method. For example:

- (void) dealloc
{
   [myMember release];
   [super dealloc];
}

Calling the base class initializer and deallocator[edit]

When a class is derived from some other class (as the MyClass was derived from NSObject), the instances created from it are not only instances of the derived class, but also instances of the base class. This is called the is-a relation. A derived class is an extension of the base class: it can do more than the base class, but it can do everything the base class can. Therefore, objects of a derived class always contain everything that objects of the base class contain. If an NSObject contains a retain count member variable, then also MyClass objects will contain it. Thus, the "base class part" of a derived object needs to be initialized and deallocated when initializing and deallocating the derived object.

To initialize the base class part of an object write

self = [super init];

to the very beginning of the initializer of your class. To deallocate the base class part of the object write

[super dealloc];

to the very end of the deallocator of your class.

More information[edit]

For more information on Objective-C in Cocoa, see