Graphics Programs Reference
In-Depth Information
{
[self doMoreStuff];
[super someMethod];
}
How does super work? Usually when you send a message to an object, the search for a
method of that name starts in the object's class. If there is no such method, the search con-
tinues in the superclass of the object. The search will continue up the inheritance hier-
archy until a suitable method is found. (If it gets to the top of the hierarchy and no method
is found, an exception is thrown.)
When you send a message to super , you are sending a message to self , but the search
for the method skips the object's class and starts at the superclass. In the case of
BNRItem 's designated initializer, we send the init message to super . This calls
NSObject 's implementation of init .
If an initializer message fails, it will return nil . Therefore, it is a good idea to save the
return value of the superclass's initializer into the self variable and confirm that it is not
nil before doing any further initialization. In BNRItem.m , edit your designated initial-
izer to confirm the initialization of the superclass.
- (id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
// Call the superclass's designated initializer
self = [super init];
// Did the superclass's designated initializer succeed?
if (self) {
// Give the instance variables initial values
[self setItemName:name];
[self setSerialNumber:sNumber];
[self setValueInDollars:value];
dateCreated = [[NSDate alloc] init];
}
// Return the address of the newly initialized object
return self;
}
Search WWH ::




Custom Search