05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

+ (BMPerson *)personWithName:(NSString *)aName age:(NSUInteger)anAge;<br />

That makes starting an implementation as easy as copying and pasting. Technically, method<br />

declarations in the implementation section don’t require semicolons, but the compiler conveniently<br />

ignores them.<br />

The factory method has a simple implementation. You allocate a new person, set its name<br />

and age, and return it. The body of a method begins and ends with curly braces:<br />

{<br />

}<br />

BMPerson *newPerson = [[self alloc] init];<br />

[newPerson setName:aName];<br />

[newPerson setAge:anAge];<br />

return [newPerson autorelease];<br />

This is just a convenient shortcut for doing the same thing in code, but there are a few subtle<br />

differences that are worth noting.<br />

If you actually instantiated a BMPerson externally, you would do so by sending alloc to the<br />

class:<br />

BMPerson *newPerson = [[BMPerson alloc] init];<br />

CHAPTER 26 MAC <strong>OS</strong> X DEVELOPMENT: OBJECTIVE-C 491<br />

From within the class method, however, you send the message to self.<br />

Behind the scenes, every method in Objective-C is converted into a function. When you send<br />

a message to an object, the messaging system looks up the appropriate function and calls it.<br />

Along with the arguments you provided in the message, a few more arguments are slipped in.<br />

The first “secret” argument is a pointer to the object on which the function is being called. That<br />

argument’s name is self.<br />

As such, whenever an object needs to refer to itself, such as to call one of its own methods,<br />

it simply sends a message to self. In an instance method, self refers to the instance, which is to<br />

say, the object. Six different objects will get six different results by sending the same message<br />

to self. In a class method, self refers to the class. Therefore, when a class calls its own class<br />

method, such as alloc, it sends a message to itself, using self. It’s a fine point, but one to keep<br />

in mind.<br />

NOTE Objects also have a secret ivar called isa that refers to its class. Class methods can<br />

therefore be called by sending a message to isa. However, runtime considerations make calling<br />

isa a gamble, so it’s better to get your class indirectly, via [self class].<br />

After setting the name and age that were passed in, you return the new object with the standard<br />

C keyword return. Because you created it, you need to release it, but because you’re<br />

returning it, you send it an autorelease message, as detailed in the previous section on memory<br />

management.<br />

To use this method in code, you send a message to the class:<br />

BMPerson *worldsToughestProgrammer = [BMPerson personWithName:@"Mike Lee" age:31];<br />

This is equivalent to creating an instance on your own, using almost the same code as you<br />

used to implement the method.<br />

BMPerson *worldsToughestProgrammer = [[BMPerson alloc] init];<br />

[newPerson setName:@"Mike Lee"];<br />

[newPerson setAge:31];

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!