-2

I am attempting to add objects to NSMutableArray "allItems1"

for (PMGWine *w in [[PMGWineStore sharedStore]allItems]) {

    [allItems1 addObject:w];

    NSLog(@"%@", w);

}

    NSLog(@"%d", [allItems1 count]);

[[PMGWineStore sharedStore]allItems] consists of 15 objects which print out perfectly in the first NSLog statement. But [allItems1 count] shows 0. What am I doing wrong?

2
  • did you allocated allItems1 ?
    – Midhun MP
    Jan 4, 2013 at 17:42
  • I declared an NSMutableArray *allItems1.
    – Pete
    Jan 4, 2013 at 17:43

2 Answers 2

0

The issue is you are not allocated the allItems1 array.

Please add this line before the for loop.

allItems1 = [[NSMutableArray alloc] init];

Also you can use:

allItems1 = [[NSMutableArray arrayWithArray:[[PMGWineStore sharedStore] allItems]] retain];

or

allItems1 = [[PMGWineStore sharedStore] allItems] copy];
0
0

You probably forgot to initialize allItems1 NSMutableArray. Before the for you write

allItems1 = [[NSMutableArray alloc] init];

You could also have written:

allItems1 = [NSMutableArray arrayWithArray:[[PMGWineStore sharedStore]allItems]];

instead of the for cycle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.