oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

The Objective-C Language
Pages: 1, 2, 3

Defining new classes in Objective-C

Now I want to talk to you about how new classes are defined in Objective-C source code. In the true spirit of the separation of interface from implementation, Objective-C defines new classes in two separate files -- an interface file and an implementation file.

The interface contains all the information required to use the class, including declarations of all instance variables and definitions of all the methods. Programmers using classes look to the interface when they want to know what a class is capable of doing. The implementation file actually contains the source code that defines how the methods are implemented.

The interface and the implementation of a class are generally separated into two files, although this practice is not mandated by the compiler. Implementation files are suffixed by a .m, which is the extension for all Objective-C source files. The interface file has the extension .h, which is typical of header files in C. Class file names typically carry the same name as the class, although this too is not required by the compiler. Thus a hypothetical "Circle" class would be defined in the files Circle.h and Circle.m.

The interface file

The interface file declares what instance variables and methods make up a class. The structure of an interface file is:

@interface ClassName : ItsSuperclass
	instance variable declerations
method declerations

Interface declarations always begin with the @interface compiler directive and end with the @end compiler directive. Following the @interface directive on the first line is the class name. A colon follows the class name and after that is the class from which your new class inherits -- its superclass. If you fail to link your class to a superclass, then it is assumed that you are creating a new root class, such as NSObject in Cocoa.

Instance variable declarations follow the first line enclosed in braces. These are the data structures of the class that the methods have to work with. For our hypothetical Circle class the variable declarations might be

double radius;
double xLocation;
double yLocation;
NSColor *color;

Instance variable declarations are always preceded by the data type. NSColor, as we will learn later, is an Application Kit class that allows us to work with colors.

Following the instance variable declarations are the method declarations. Like standard C functions, methods in Objective-C have a return type and can take arguments. Additionally, methods can either be class methods or instance methods. Class methods can only be performed by the class object discussed above, and instance methods can be performed by any object of the class. A plus sign before the method name indicates that the method is a class method.

+ alloc

Method declarations that begin with a minus sign are instance methods

- (void)setXLocation:(double)x Ylocation:(double)y;

The return type of the method is enclosed in parentheses before the actual method name. Arguments follow colons and multiple arguments are separated by the argument name and a space. Like method return types, argument data types are also enclosed in parentheses before the name of the argument.

For a class to link to a superclass you must import the superclass's interface file, which usually is the superclass name followed by a .h.

#import "ItsSuperclass.h"

The #import statement serves the exact same purpose as the #include statement many of you are probably familiar with from C -- only it's smarter. The key advantage to #import is that it knows if a file has already been included, and will not bring in duplicates; #include does not check for this.

Pages: 1, 2, 3

Next Pagearrow