Fast Enumeration P.57


#1

Having an issue with the code on p. 57.

Here is what the code looks like before getting to Fast Enumeration

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

@autoreleasepool {
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++) {
        BNRItem *p = [BNRItem randomItem];[items addObject:p];
    }
    
    for (int i = 0; i < [items count]; i++) {
        NSLog(@"%@", [items objectAtIndex:i]);
        
    }
    items = nil;
    
return 0;

The book tells me to delete this code which isn’t found in the Main.m file.
for (int)i = 0; i < [items count]; i++) {
BNRItem *item = [items objectAtIndex:i];
NSLog(@"%@", item);


#2

I have tried many combinations and the code we are directed to insert into the main.m does not produce a list.
NEED SOME HELP… please

//        // Created to use randomItem
//        for (int i = 0; i < 10; i++)
//        {
//            BNRItem *p = [BNRItem randomItem];
//            [items addObject:p];
//            NSLog(@"%@", p);
//        }
//

        
        
        // fast enumeration replaces for loop above
        for (BNRItem *item in items) {
            NSLog(@"%@", item);
        }

#3

You have commented out the code which actually creates the BNRItem instances which you then loop through using the fast enumeration code section.

The book is slightly misleading on Page 57, as the code it asks you to delete was previously used on Page 55, however does not exactly match that on Page 57:

Page 55:

for (int i = 0; i < [items count]; i++) {
    NSLog(@"%@", [items objectAtIndex:i]);
}

Page 57:

for (int i = 0; i < [items count]; i++) {
    BNRItem *item = [items objectAtIndex:i];
    NSLog(@"%@", item);
}

Nick


#4

Nick,
Thank you but I am concerned. I am using a Kindle addition of this book and in my book page 55 code is

       // Created to use randomItem
        for (int i = 0; i < 10; i++)
        {
            BNRItem *p = [BNRItem randomItem];
            [items addObject:p];
        }

        for (int i =0; i < [items count]; i++)
        {
            NSLog(@"%@", [items objectAtIndex:i]);
        }

This code was introduced at the top of page 57 but I was not told to add it to my project. It was part of a discussion about prior to versions:
“Before Objective-C 2.0, we iterated through arrays the way you did in your main function:”


for (int i = 0; i < [items count]; i + +) { 
	BNRItem *item = [items objectAtIndex:i]; 
	NSLog(@"%@", item); 
}

So my concern is my Kindle Version not the same as the printed version of this book? I can add the two pieces of code you are suggesting but this not helping be confident in the Kindle Version I am using.
I love these book they are helping me learn iOS/Object-C programming. Can someone at BNR help me with my concerns??

So this combination of code works an all comes from my version of the book.

        // Created to use randomItem (page 55)
        for (int i = 0; i < 10; i++)
        {
            BNRItem *p = [BNRItem randomItem];
            [items addObject:p];
        }

//        for (int i =0; i < [Items Count]; i++)
//        {
//            NSLog(@"%@", [items objectAtIndex:i]);
//        }
        
        // fast enumeration replaces for loop's above (page 57)
        for (BNRItem *item in items) {
            NSLog(@"%@", item);
        }