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 B

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
code that initializes the BNRItem and the code sets its instance variables with a single message send using the designated initializer. Also, get rid of the code that populates the NSMutableArray with strings and prints them to the console. In main.m , make the following changes:
     
    #import
#import "BNRItem.h"

int main (int argc, const char * argv[])
{
    @autoreleasepool {

        NSMutableArray *items = [[NSMutableArray alloc] init];
         [items addObject:@"One"];
         [items addObject:@"Two"];
         [items addObject:@"Three"];
         [items insertObject:@"Zero" atIndex:0];

         for (int i = 0; i < [items count]; i++) {
             NSLog(@"%@", [items objectAtIndex:i]);
         }
         BNRItem *p = [[BNRItem alloc] init];
         NSLog(@"%@ %@ %@ %d", [p itemName], [p dateCreated],
                               [p serialNumber], [p valueInDollars]);
                 BNRItem *p = [[BNRItem alloc] initWithItemName:@"Red Sofa"
                                        valueInDollars:100
                                          serialNumber:@"A1B2C"];

        NSLog(@"%@", p);
        items = nil;
    }
    return 0;
}
    Build and run the application. Notice that the console now prints a single BNRItem that was instantiated with the values passed to BNRItem ’s designated initializer.
     
    Class methods
    Methods come in two flavors: instance methods and class methods. Instance methods (like init ) are sent to instances of the class, and class methods (like alloc ) are sent to the class itself. Class methods typically either create new instances of the class or retrieve some global property of the class. Class methods do not operate on an instance or have any access to instance variables.
     
    Syntactically, class methods differ from instance methods by the first character in their declaration. An instance method uses the - character just before the return type, and a class method uses the + character.
     
    One common use for class methods is to provide convenient ways to create instances of that class. For the BNRItem class, it would be nice if you could create a random item so that you could test your class without having to think up a bunch of clever names. In BNRItem.h , declare a class method that will create a random item.
     
    @interface BNRItem : NSObject
{
    NSString *itemName;
    NSString *serialNumber;
    int valueInDollars;
    NSDate *dateCreated;
}
+ (id)randomItem;

- (id)initWithItemName:(NSString *)name
        valueInDollars:(int)value
          serialNumber:(NSString *)sNumber;
    Notice the order of the declarations for the methods. Class methods come first, followed by initializers, followed by any other methods. This is a convention that makes your header files easier to read.
     
    In BNRItem.m , implement randomItem to create, configure, and return a BNRItem instance. (Again, make sure this method is between the @implementation and @end .)
     
    + (id)randomItem
{
    // Create an array of three adjectives
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy",
                                                             @"Rusty",
                                                             @"Shiny", nil];

    // Create an array of three nouns
    NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear",
                                                        @"Spork",
                                                        @"Mac", nil];

    // Get the index of a random adjective/noun from the lists
    // Note: The % operator, called the modulo operator, gives
    // you the remainder. So adjectiveIndex is a random

Similar Books

Skin Walkers - King

Susan Bliler

A Wild Ride

Andrew Grey

The Safest Place

Suzanne Bugler

Women and Men

Joseph McElroy

Chance on Love

Vristen Pierce

Valley Thieves

Max Brand