-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?

Pete
  • 613
  • 1
  • 6
  • 18

2 Answers2

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];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
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.

Levi
  • 7,313
  • 2
  • 32
  • 44