Graphics Programs Reference
In-Depth Information
#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]);
items = nil;
}
return 0;
}
Build and run the application. Check the console by selecting the entry at the top of the
log navigator. At the end of the console output, you should see a line that has three
“(null)” strings and a 0. When an object is created, all of its instance variables are set to 0.
For pointers to objects, that pointer points to nil ; for primitives like int , the value is 0.
To give this BNRItem some substance, you need to create new objects and pass them as
arguments to the setter methods for this instance. In main.m , type in the following code:
// Notice we omitted some of the surrounding code. The bold code is the code to
add,
// the non-bold code is existing code that shows you where to type in the new stuff.
BNRItem *p = [[BNRItem alloc] init];
// This creates a new NSString, "Red Sofa" and gives it to the BNRItem
[p setItemName:@"Red Sofa"];
// This creates a new NSString, "A1B2C" and gives it to the BNRItem
[p setSerialNumber:@"A1B2C"];
// We send the value 100 to be used as the valueInDollars of this BNRItem
[p setValueInDollars:100];
NSLog(@"%@ %@ %@ %d", [p itemName], [p dateCreated],
[p serialNumber], [p valueInDollars]);
Build and run the application. Now you should see values for everything but the
dateCreated , which we'll take care of shortly.
Search WWH ::




Custom Search