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

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
Ads: Link
declared.
     
    Instance variables
    So far, the BNRItem class doesn’t add anything to its superclass NSObject . It needs some item-like instance variables. An item, in our world, is going to have a name, a serial number, a value, and a date of creation.
     
    Instance variables for a class are declared in between curly brackets immediately after the class declaration. In BNRItem.h , add four instance variables (and the curly brackets that contain them) to the BNRItem class:
     
    #import

@interface BNRItem : NSObject {
    NSString *itemName;
    NSString *serialNumber;
    int valueInDollars;
    NSDate *dateCreated;
}

@end

     
    Now every instance of BNRItem has one spot for a simple integer and three spots to hold references to objects, specifically two NSString instances and one NSDate instance. (A reference is another word for pointer; the * denotes that the variable is a pointer.) Figure 2.12 shows an example of a BNRItem instance after its instance variables have been given values.
     
    Figure 2.12  A BNRItem instance
     
    Notice that Figure 2.12 shows a total of four objects: the BNRItem , two NSString s, and the NSDate . Each of these objects is its own object and exists independently of the others. The BNRItem only has pointers to the three other objects. These pointers are the instance variables of BNRItem .
     
    For example, every BNRItem has a pointer instance variable named itemName . The itemName of the BNRItem shown in Figure 2.12 points to an NSString instance whose contents are “ Red Sofa. ” The “ Red Sofa ” string does not live inside the BNRItem , though. The BNRItem instance knows where the “ Red Sofa ” string lives in memory and stores its address as itemName . One way to think of this relationship is “ the BNRItem calls this string its itemName . ”
     
    The story is different for the instance variable valueInDollars . This instance variable is not a pointer to another object; it is just an int . Non-pointer instance variables are stored inside the object itself. The idea of pointers is not easy to understand at first. In the next chapter, you’ll learn more about objects, pointers, and instance variables, and, throughout this book, we will make use of object diagrams like Figure 2.12 to drive home the difference between an object and a pointer to an object.
     
    Accessor methods
    Now that you have instance variables, you need a way to get and set their values. In object-oriented languages, we call methods that get and set instance variables accessors . Individually, we call them getters and setters . Without these methods, an object cannot access the instance variables of another object.
     
    Accessor methods look like this:
     
    // a getter method
- (NSString *)itemName
{
    // Return a pointer to the object this BNRItem calls its itemName
    return itemName;
}

// a setter method
- (void)setItemName:(NSString *)newItemName
{
    // Change the instance variable to point at another string,
    // this BNRItem will now call this new string its itemName
    itemName = newItemName;
}

     
    Then, if you wanted to access (set or get) a BNRItem ’s itemName , you would send the BNRItem one of these messages:
     
    // Create a new BNRItem instance
BNRItem *p = [[BNRItem alloc] init];

// Set itemName to a new NSString
[p setItemName:@"Red Sofa"];

// Get the pointer of the BNRItem's itemName
NSString *str = [p itemName];

// Print that object
NSLog(@"%@", str); // This would print "Red Sofa"

     
    In Objective-C, the name of a setter method is set plus the name of the instance variable it is changing – in this case, setItemName: . In other languages, the name of the getter method would likely be getItemName . However, in Objective-C, the name of the getter method is just the name of the instance variable. Some of the cooler parts of the Cocoa Touch library make the assumption that your classes follow this convention; therefore,

Similar Books

Overdrive

Chloe Cole

Dream Paris

Tony Ballantyne

Crave

Jordan Sweet