Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [15]

By Root 773 0
init];

What about destroying the object when we no longer need it? We will talk about this in the next chapter.

Some methods take arguments. If a method takes an argument, the method name (called a selector) will end with a colon. For example, to add objects to the end of the array, you use the addObject: method (assume that bar is a pointer to another object):

[foo addObject:bar];

If you have multiple arguments, the selector will have multiple parts. For example, to add an object at a particular index, you could use the following:

[foo insertObject:bar atIndex:5];

Note that insertObject:atIndex: is one selector, not two. It will trigger one method with two arguments. This outcome seems strange to most C and Java programmers but should be familiar to Smalltalk programmers. The syntax also makes your code easier to read. For example, it is not uncommon to see a C++ method call like this:

if (x.intersectsArc(35.0, 19.0, 23.0, 90.0, 120.0))

It is much easier to guess the meaning of the following code:

if ([x intersectsArcWithRadius:35.0

centeredAtX:19.0

Y:23.0

fromAngle:90.0

toAngle:120.0])

If it seems odd right now, just use it for a while. Most programmers grow to appreciate the Objective-C messaging syntax.

You are now at a point where you can read simple Objective-C code, so it is time to write a program that will create an instance of NSMutableArray and fill it with ten instances of NSNumber.

Using Existing Classes


If it isn’t running, start Xcode. Close any projects that you were working on. Under the File menu, choose New -> New Project.... When the panel pops up, choose to create a Command Line Tool (Figure 3.1).

Figure 3.1. Choose Project Type

A command-line tool has no graphical user interface and typically runs on the command line or in the background as a daemon. Unlike in an application project, you will always alter the main function of a command-line tool.

Name the project lottery (Figure 3.2). Unlike the names of applications, most tool names are lowercase. Set the Type to Foundation.

Figure 3.2. Name Project

When the new project appears, select main.m in the lottery group. Edit main.m to look like this:

#import

int main (int argc, const char * argv[])

{

@autoreleasepool {

NSMutableArray *array;

array = [[NSMutableArray alloc] init];

int i;

for (i = 0; i < 10; i++) {

NSNumber *newNumber =

[[NSNumber alloc] initWithInt:(i * 3)];

[array addObject:newNumber];

}

for ( i = 0; i < 10; i++) {

NSNumber *numberToPrint = [array objectAtIndex:i];

NSLog(@"The number at index %d is %@", i, numberToPrint);

}

}

return 0;

}

Here is the play-by-play for the code:

#import

You are including the headers for all the classes in the Foundation framework. The headers are precompiled, so this approach is not as computationally intensive as it sounds.

int main (int argc, const char *argv[])

The main function is declared just as it would be in any Unix C program.

@autoreleasepool {

This code defines an autorelease pool for the code enclosed by the braces. We will discuss the importance of autorelease pools in the next chapter.

NSMutableArray *array;

One variable is declared here: array is a pointer to an instance of NSMutableArray. Note that no array exists yet. You have simply declared a pointer that will refer to the array once it is created.

array = [[NSMutableArray alloc] init];

Here, you are creating the instance of NSMutableArray and making the array variable point to it.

for (i = 0; i < 10; i++) {

NSNumber *newNumber = [[NSNumber alloc] initWithInt:(i*3)];

[array addObject:newNumber];

}

Inside the for loop, you have created a local variable called newNumber and set it to point to a new instance of NSNumber. Then you have added that object to the array.

The array does not make copies of the NSNumber objects. Instead, it simply keeps a list of pointers to the NSNumber objects. Objective-C programmers make very few copies

Return Main Page Previous Page Next Page

®Online Book Reader