Graphics Programs Reference
In-Depth Information
This method's name, or selector, is
initWithItemName:valueInDollars:serialNumber: . This selector has
three labels ( initWithItemName: , valueInDollars: , and serialNumber: ),
which tells you that the method accepts three arguments.
These arguments each have a type and a parameter name. In the declaration, the type fol-
lows the label in parentheses. The parameter name then follows the type. So the label
initWithItemName: is expecting a pointer to an instance of type NSString . Within
the body of this method, you can use name to reference the NSString object pointed to.
id
Take another look at the initializer's declaration; its return type is id (pronounced “eye-
dee”). This type is defined as “a pointer to any object.” ( id is a lot like void * in C.)
init methods are always declared to return id .
Why not make the return type BNRItem * ? After all, that is the type of object that is re-
turned from this method. A problem will arise, however, if BNRItem is ever subclassed.
The subclass would inherit all of the methods from BNRItem , including this initializer
and its return type. An instance of the subclass could then be sent this initializer message,
but what would be returned? Not a BNRItem , but an instance of the subclass. You might
think, “No problem. Override the initializer in the subclass to change the return type.” But
in Objective-C, you cannot have two methods with the same selector and different return
types (or arguments). By specifying that an initialization method returns “any object,” we
never have to worry what happens with a subclass.
isa
As programmers, we always know the type of the object that is returned from an initial-
izer. (How do we know this? It is an instance of the class we sent alloc to.) The object
itself also knows its type - thanks to its isa pointer.
Every object has an instance variable called isa . When an instance is created by sending
alloc to a class, that class sets the isa instance variable of the returned object to point
back at the class that created it ( Figure 2.13 ). We call it the isa pointer because an object
“is a” instance of that class.
Search WWH ::




Custom Search