Cocoa Programming/Objective-C basics

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
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,. All Cocoa objects should be descendants of  in order to gain from its improved memory management model.

is defined in the header.

The NSObject life cycle
To create an, send its class either an  or a   message:

Although " " and " followed by  " methods appear to be identical, Apple documentation seems to give preference to the latter method. This gives developers the ability to override  or   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,  will return. Therefore, it is always necessary to check the return value of  before using the object!

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

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  message.

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  message. If the object has reserved any resources, it should release them now.

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


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

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

Defining initializer and deallocator
An initializer is a method that initializes an object. In Cocoa, the initializer for  is called. The  method for   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  (i.e., you should override the base class initializer). This makes it possible to create instances of your class by using the  class method derived from  :

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

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

and implement it in the.m file:

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

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

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. Every class that holds any resources should define a  method. For example:

Calling the base class initializer and deallocator
When a class is derived from some other class (as the  was derived from  ), 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  contains a retain count member variable, then also   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

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

to the very end of the deallocator of your class.

More information
For more information on Objective-C in Cocoa, see
 * Objective-C Programming for general information on Objective-C
 * Objective-C GNUstep Base Programming Manual for specific information on Objective-C in Cocoa/GNUstep.