Graphics Programs Reference
In-Depth Information
it contains text and a number of tokens. The tokens (also called format specifications) are
prefixed with a percent symbol ( % ), and each additional argument passed to the function
replaces a token in the format string. Tokens also specify the type of the argument they
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 descrip-
tion . The description method returns an NSString that replaces the token. Be-
cause 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 ob-
ject (also called a container). The Cocoa Touch frameworks provide a handful of collec-
tion 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, re-
trieve 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.
Search WWH ::




Custom Search