macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

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

The implementation file

The implementation file is where the meat of your class is coded. This file contains all the source code that gives your methods substance -- that allows them to do something useful. Remember now that the interface file is where your class is described, and this is the description programmers are to use to incorporate your class into their code. The implementation file is where you do your work.



The implementation file follows a similar format as the interface file, as shown below.

#import "ClassName.h"

@implementation ClassName : ItsSuperclass
{
	instance variable declarations
}
method definitions
@end

Every class implementation must import its own interface file. Because the interface declares the supeclass and the superclass from which your class inherits, these things can be omitted from the implementation file. This reinforces the concept of the implementation being purely devoted fleshing out the methods. Thus, in practice, the implementation file only need contain the following code:

#import "ClassName.h"

@implementation ClassName
method definitions
@end

Methods are defined much like C functions are defined. The name is written exactly as is done in the interface file (without the semicolon), and method implementation code is contained in a pair of braces following the name. For example, our Circle class might have the following methods:

Learning CocoaLearning Cocoa
By Apple Computer, Inc.
Table of Contents
Index
Sample Chapter
Full Description
Read Online -- Safari

+ alloc
{
	your code
}

- (void)setXLocation:(double)x yLocation:(double)y
{
	your code
}

You may be wondering why + alloc has not returned the data type. The default return data type in Objective-C is type id -- that is, an object is returned by default. The + alloc method is designed to return an object of the class, and thus no return type need be specified. While we're on the topic of the alloc method, I should mention that you will rarely need to implement + alloc on your own. The NSObject class takes care of this. + alloc's purpose is to allocate (hence the name) memory space for newly created objects. The following sections will go into more detail about creating new objects.

Instance variables can be referred to in your method definitions directly. All instance variables are within the scope of each method without explicitly saying so. They are analagous to global variables in C. Thus, we could simply define the second method above as follows:


- (void)setXLocation:(double)x yLocation:(double)y
{
	xLocation = x;
	yLocation = y;
}
It is also possible to have local variables with more limited scope within each method. For example, in the second method above, we could insert a useless intermediary variable:

- (void)setXLocation:(double)x yLocation:(double)y
{
	double tempX;
	double tempY;

	tempX = x;
	tempY = y;

	xLocation = tempX;
	yLocation = tempY;
}

If I may go out on a limb, you could say that anything that applies to standard C functions, also applies to methods of a class.

Creating new objects

New objects are created by sending an alloc message to the class object of the class you wish to make an instance of. For example, if we wanted to create an instance of the Circle class, we would do the following:


id aCircle;
aCircle = [Circle alloc];

Remember, alloc returns an object, so the variable that refers to the instance you're creating must also be of type id. Once a new instance has been created we must also initialize its instance variables. This is done by issuing an init message to the newly created object.


[aCircle init];

Initialization must occur directly after allocation, so it's more common to create new objects by nesting the two messages above.


aCircle = [[Circle alloc] init];

By default, init initializes all the instance variables to zero. You are free to create your own initialization methods, called constructors, to initialize the variables as you see fit. By default constructors begin with "init". Because a constructor must interact with instance variables of an object, it must be an instance method, rather than a class method like alloc. For example, you might have a method in our circle class to initialize the circle with a radius of 10. This method would look like this:


- (void)initWithRadius:(double)r;
{
	radius = r;
}

Any instance variables not initialized in your custom constructor will be set to zero by default.

In Project Builder

Also in Programming With Cocoa:

Understanding the NSTableView Class

Inside StYNCies, Part 2

Inside StYNCies

Build an eDoc Reader for Your iPod, Part 3

Build an eDoc Reader for your iPod, Part 2

I haven't talked about Project Builder just yet, but I just wanted to touch on a feature that relates to the current discussion. Project Builder contains templates for class interface and implementation files. What you get is a skeleton of a class that inherits from NSObject in the Foundation Framework -- all you have to do is flesh it out with your data structures and methods. So, now you both understand the struture of a class in source code, and how to make one with less work.

It won't be long now before we finish up with the nuts and bolts and get into programming real applications. Next time, I will introduce you to Interface Builder. Our foray into Interface Builder will be largely experiential. In the next three columns, I will go through two complete applications where you will see how Interface Builder and Project Builder work together to make your programming more enjoyable. See you then!