iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides)
correspond to. Here’s an example:
     
    int a = 1;
float b = 2.5;
char c = 'A';
NSLog(@"Integer: %d Float: %f Char: %c", a, b, c);
    The order of the arguments matters: the first token is replaced with the second argument (the format string is always the first argument), the second token is replaced with the third argument, and so on. The console output would be
     
    Integer: 1 Float: 2.5 Char: A

     
    In C, there is a function called printf that does the same thing. However, NSLog adds one more token to the available list: %@ . The type of the argument this token responds to is “ any object. ” When %@ is encountered in the format string, instead of the token being replaced by the corresponding argument, that argument is sent the message description . The description method returns an NSString that replaces the token. Because the argument is sent a message, that argument must be an object. As we’ll see shortly, every object implements the method description , so any object will work.
     
    NSArray and NSMutableArray
    What exactly is this NSMutableArray object you’re using? An array is a collection object (also called a container). The Cocoa Touch frameworks provide a handful of collection objects, including NSDictionary and NSSet , and each has a slightly different use. An array is an ordered list of objects, and these objects can be accessed by an index. Other languages might call it a list or a vector. An NSArray is immutable, which means you cannot add or remove objects after the array is instantiated. You can, however, retrieve objects from the array. NSArray ’s mutable subclass, NSMutableArray , lets you add and remove objects dynamically.
     
    In Objective-C, an array does not actually contain the objects that belong to it; instead it holds a pointer to each object. When an object is added to an array,
     
        [array addObject:object];
    the address of that object in memory is stored inside the array.
     
    So, to recap, in your command line tool, you created an instance of NSMutableArray and added four instances of NSString to it, as shown in Figure 2.7 .
     
    Figure 2.7  NSMutableArray instance
     
    Arrays can only hold references to Objective-C objects. This means primitives and C structures cannot be added to an array. For example, you cannot have an array of int s. Also, because arrays hold pointers to objects, a single array can contain objects of different types . This is different from most strongly-typed languages where an array can only hold objects of its declared type.
     
    You can ask an array how many objects it is currently storing by sending it the message count .
     
        int numberOfObjects = [array count];
    This information is important because if you ask for an object from an array at an index that is greater than the number of objects in the array, an exception will be thrown. (Exceptions are very bad; they will most likely cause your application to crash. We’ll talk more about exceptions at the end of this chapter.)
     
    When an object is added to an array with the message addObject: , it is added at the end of the array. You can also insert objects at a specific index – as long as that index is less than or equal to the current number of objects in the array.
     
        int numberOfObjects = [array count];
    [array insertObject:object
                atIndex:numberOfObjects];

     
    Note that you cannot add nil to an array. If you need to add “ holes ” to an array, you must use NSNull . NSNull is an object that represents nil and is used specifically for this task.
     
        [array addObject:[NSNull null]];

     
    To retrieve the pointer to an object later, you send the message objectAtIndex: to the array.
     
        NSString *object = [array objectAtIndex:0];

     

Subclassing an Objective-C Class
    Classes, like NSMutableArray , exist in a hierarchy, and every class has exactly one superclass – except for the root class of the entire hierarchy: NSObject (

Similar Books

Silver Lies

Ann Parker

The Rising Dead

Devan Sagliani

The Mercenary Knight

Elyzabeth M. VaLey

Shadow Man: A Novel

Jeffrey Fleishman