int main(int argc, const char * argv[])
{ @autoreleasepool {
NSMutableArray *groceryList = [NSMutableArray array];
[groceryList addObject:@“Dog food”];
[groceryList addObject:@“Milk”];
[groceryList addObject:@“Dog treats”];
NSLog(@"\n\nMy grocery list is:\n\n");
for (NSString *currentItem in groceryList) {
NSLog(@"\n\n%@\n\n", currentItem);
}
}
return 0;
}[/code]
Nothing too special, seemed like a fairly simple challenge.
What I was wondering about, though, is whether there’s any way to use a loop to output the list and have it match the output listed in the book (i.e., skipping the log information that NSLog() gives). I know the book just skips that output, but I’m a bit sad I can’t make my output look nice. I tried to use printf(), but it doesn’t seem to like instances of NSString. Anything to be done about this?
Just invoke the UTF8String method on currentItem which is just an instance of NSString. (You have to change the %@ descriptor to %s in the format string.)