iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides)

Free iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) by Aaron Hillegass, Joe Conway Page A

Book: iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) by Aaron Hillegass, Joe Conway Read Free Book Online
Authors: Aaron Hillegass, Joe Conway
Tags: COM051370, Big Nerd Ranch Guides, iPhone / iPad Programming
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;
}

     
    Other initializers and the initializer chain
    A class can have more than one initializer. First, let’s consider a hypothetical example. BNRItem could have an initializer that takes only an NSString for the itemName . Its declaration would look like this:
     
    - (id)initWithItemName:(NSString *)name;
    In this initializer’s definition, you wouldn’t replicate the code in the designated initializer. Instead, this initializer would simply call the designated initializer, pass the information it was given as the itemName , and pass default values for the other arguments.
     
    - (id)initWithItemName:(NSString *)name
{
    return [self initWithItemName:name
                   valueInDollars:0
                     serialNumber:@""];
}
    Using initializers as a chain like this reduces the chance for error and makes maintaining code easier. For classes that have more than one initializer, the programmer who created the class chooses which initializer is designated. You only write the core of the initializer once in the designated initializer, and other initialization methods simply call that core with default values.
     
    Now let’s look at a real example. BNRItem actually has another initializer, init , which it inherits it from its superclass NSObject . If init is sent to an instance of BNRItem , none of the stuff you put in the designated initializer will be called. Therefore, you must link BNRItem ’s implementation of init to its designated initializer.
     
    In BNRItem.m , override the init method to call the designated initializer with default values for all of the arguments.
     
    - (id)init
{
    return [self initWithItemName:@"Item"
                   valueInDollars:0
                     serialNumber:@""];
}

     
    The relationships between BNRItem ’s initializers (real and hypothetical) are shown in Figure 2.14 ; the designated initializers are white, and the additional initializer is gray.
     
    Figure 2.14  Initializer chain
     
    Let’s form some simple rules for initializers from these ideas.
     
    A class inherits all initializers from its superclass and can add as many as it wants for its own purposes.

     
    Each class picks one initializer as its designated initializer .

     
    The designated initializer calls the superclass’s designated initializer.

     
    Any other initializer a class has calls the class’s designated initializer.

     
    If a class declares a designated initializer that is different from its superclass, the superclass’s designated initializer must be overridden to call the new designated initializer.

     
    Using Initializers
    Currently, the code in main.m sends the message init to the new instance of BNRItem . With these new initializer methods, this message will run the init method you just implemented in BNRItem , which calls the designated initializer ) initWithItemName:valueInDollars:serialNumber: ) and passes default values. Let’s make sure this works as intended.
     
    In main.m , log the BNRItem to the console after it is initialized but before the setter messages are sent.
     
    BNRItem *p = [[BNRItem alloc] init];
NSLog(@"%@", p);

// This creates a new NSString, "Red Sofa", and gives it to the BNRItem
[p setItemName:@"Red Sofa"];

     
    Build and run the application. Notice that the console spits out the following messages:
     
    Item (): Worth $0, recorded on 2011-07-19 18:56:42 +0000
Red Sofa (A1B2C): Worth $100, recorded on 2011-07-19 18:56:42 +0000

     
    Now replace the

Similar Books

Scourge of the Dragons

Cody J. Sherer

The Smoking Iron

Brett Halliday

The Deceived

Brett Battles

The Body in the Bouillon

Katherine Hall Page