Graphics Programs Reference
In-Depth Information
// Call the superclass's designated initializer
self = [super init];
// 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;
}
In the designated initializer, the first thing you always do is call the superclass's desig-
nated initializer using super . The last thing you do is return a pointer to the successfully
initialized object using self . So to understand what's going on in an initializer, you will
need to know about self and super .
self
Inside a method, self is an implicit local variable. There is no need to declare it, and it is
automatically set to point to the object that was sent the message. (Most object-oriented
languages have this concept, but some call it this instead of self .) Typically, self is
used so that an object can send a message to itself:
- (void)chickenDance
{
[self pretendHandsAreBeaks];
[self flapWings];
[self shakeTailFeathers];
}
In the last line of an init method, you always return the newly initialized object so that
the caller can assign it to a variable:
return self;
super
Often when you are overriding a method, you want to keep what the method of the super-
class is doing and have your subclass add something new on top of it. To make this easier,
there is a compiler directive in Objective-C called super :
- (void)someMethod
Search WWH ::




Custom Search