Challenge 1 question

Here’s my solution:

[code]#import <Foundation/Foundation.h>

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.)

NSLog (@"\n\n%s\n\n", [currentItem UTF8String]);

Knew it had to be something simple. Brilliant! many thanks.

[code]
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    NSString *bread = @"Loaf of bread";
    NSString *milk = @"Container of milk";
    NSString *butter = @"Stick of butter";
    
    NSMutableArray *groceryList = [NSMutableArray array];
    [groceryList addObject: bread];
    [groceryList addObject: milk];
    [groceryList addObject: butter];
    
    NSLog(@"My grocery list is:\n");
    for (NSString *groceryItem in groceryList) {
        NSLog(@"%s\n", [groceryItem UTF8String]);
    }
}
return 0;

}[/code]

The weird thing is that mine still shows the log information even thought I call UTF8String.