CLASSES

In Objective-C, you will spend a lot of time dealing with classes and objects. Hence, it is important to understand how classes are declared and defined in Objective-C.

@interface

To declare a class, you use the @interface compiler directive, like this:

@interface SomeClass : NSObject {

}

This is done in the header file (.h), and the class declaration contains no implementation. The preceding code declares a class named SomeClass, and this class inherits from the base class named NSObject.

image NOTE While you typically put your code declaration in an .h file, you can also put it inside an .m if need be. This is usually done for small projects.

image NOTE NSObject is the root class of most Objective-C classes. It defines the basic interface of a class and contains methods common to all classes that inherit from it. NSObject also provides the standard memory management and initialization framework used by most objects in Objective-C, as well as reflection and type operations.

In a typical View Controller class, the class inherits from the UlViewController class, such as in the following:

@interface HelloWorldViewController : UIViewController {

}

@implementation

To implement a class declared in the header file, you use the @implementation compiler directive, like this:

#import ...

Get Beginning iOS 5 Application Development 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.