Something and nothing
NSValue
Many Cocoa classes, like NSArray and NSValue only takes objects as parameters. What to do when you have a non-object value you want to store? Simple, use NSValue.
Let’s say we want to store a [CGRect struct] [1] into an array.
CGRect rect = CGRectMake(0, 0, 300, 300);
NSValue *rectValue = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
NSArray *array = [NSArray arrayWithObject:rectValue];
NSLog(@"array: %@", array); /* => array: ( "NSRect: {{0, 0}, {300, 300}}" ) */
Later, you can retrieve the non-object value with
CGRect rect2;
[rectValue getValue:&rect2];
Observe the use of the address operator (&) here.
NSNull, nil, NULL
Sometimes you have plenty, sometime you don’t. In plain vanilla C you have NULL, a special pointer that points to nowhere. nil is just the same, but, by convention, it is used not for primitives, like NULL but for objects. For instance
someObject = nil;
Will point someObject ref to a bit bucket.
nil is also used as the sentinel in the factory methods of NSDictionary, NSArray and NSSet. If you need a null object to pass around, Cocoa provides NSNull
NSNull *nullValue = [NSNull null];
NSArray *array = [NSArray arrayWithObjects:nullValue, nil];
[1]: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/Reference/Reference.html#//apple_ref/occ/clm/NSValue/valueWithRect: “A CGRect is a little contrived, because NSValue offers a special API for that particular structure.