2

Possible Duplicate:
NSMutableArray addObject not affecting count?

I'm creating a search function that searches the NSArray catalogArray for any instance of a particular string defined in searchMe.text. Once a match is discovered, I'd like to add the objects to the mutable array searchResultMA, but so far I get null results.

Here's my search method:

-(IBAction)searchFor{
    int i=0;
    for (int j=0; j<[catalogMA count]/170; j++) {
        NSRange rangeArray = NSMakeRange(j*170, 170);
        catalogArray = [catalogMA subarrayWithRange:rangeArray];
        if([catalogArray containsObject:searchMe.text]) {
            NSLog(@"catalogArray count=%d", [catalogArray count]);
            [searchResultMA addObjectsFromArray:catalogArray];
            NSLog(@"%d, %@", i, [searchResultMA objectAtIndex:9+i*170]);
            i++;
        }
    }
}

and, here is my console output when I search for a string that I know has two instances in the array:

2013-01-08 18:07:05.300 Catalog[3229:c07] catalogArray count=170
2013-01-08 18:07:05.301 Catalog[3229:c07] 0, (null)
2013-01-08 18:07:05.301 Catalog[3229:c07] catalogArray count=170
2013-01-08 18:07:05.302 Catalog[3229:c07] 1, (null)

Why do I get a (null) result from searchResultsMA?

When I add in the line NSLog(@"%d, %d", i, [searchResultMA count]); within the conditional, I get 0,0 and 1,0, which tells me that there's nothing in the searchResultMA mutable array. It was supposed to be 0,170 and 1,340. Thoughts? Tips? Suggestions? All welcome...

Community
  • 1
  • 1
Peter Brockmann
  • 3,594
  • 3
  • 28
  • 28
  • 2
    `searchResultsMA` is `nil`. You need to create it. Possible duplicate of [Having trouble adding objects to NSMutableArray](http://stackoverflow.com/q/851926) [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/q/7125326), [\[NSMutableArray addObject:\] not affecting count](http://stackoverflow.com/q/3683761), [\[NSMutableArray addObject:\] not working](http://stackoverflow.com/q/1827058) – jscs Jan 08 '13 at 23:25
  • Brilliant Josh! Duh. That's it exactly. Thanks a lot. – Peter Brockmann Jan 08 '13 at 23:28

0 Answers0