iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides)
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.
     
    Figure 2.13  The isa pointer
     
    The isa pointer is where Objective-C gets much of its power. At runtime, when a message is sent to an object, that object goes to the class named in its isa pointer and says, “ I was sent this message. Run the code for the matching method. ” This is different than most compiled languages, where the method to be executed is determined at compile time.
     
    Implementing the designated initializer
    Now that you have declared the designated initializer in BNRItem.h , you need to implement it. Open BNRItem.m . Recall that the definitions for methods go within the implementation block in the implementation file, so add the designated initializer there.
     
    @implementation BNRItem
- (id)initWithItemName:(NSString *)name
        valueInDollars:(int)value
          serialNumber:(NSString *)sNumber
{
    // 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 designated 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 superclass 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
{
    [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 continues in the superclass of the object. The search will continue up the inheritance hierarchy 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 initializer to confirm the initialization of the superclass.
     
    - (id)initWithItemName:(NSString *)name
        valueInDollars:(int)value
          serialNumber:(NSString *)sNumber
{
    // Call the superclass's

Similar Books

Witching Hill

E. W. Hornung

Beach Music

Pat Conroy

The Neruda Case

Roberto Ampuero

The Hidden Staircase

Carolyn Keene

Immortal

Traci L. Slatton

The Devil's Moon

Peter Guttridge