Scar
Scar

Reputation: 3480

NSMutableArray removeAllObjects issue

I'm facing a problem with NSMutableArray that give me a Exc_Bad_Access without a reason.

I have a UITableView that contain around 700 records, i wanna to activate a filter process to it, i'm using the following method to filter the content of the UITableView:

- (void) filterTableContentWith:(int)_minValue andWith:(int)_maxValue {

       [tableContent removeAllObjects];
       tableContent = [[NSMutableArray alloc] initWithArray:originalTableContent copyItems:YES];

       if (_minValue == 0 && _maxValue == 0) {
            NSLog(@"This is mean that no filter is activate here");
       } else {
            for (int x = ([tableContent count] - 1); x >= 0; x--) {
                 if ([[[tableContent objectAtIndex:x] objectForKey:@"price"] intValue] < _minValue && [[[tableContent objectAtIndex:x] objectForKey:@"price"] intValue] > _maxValue) {
                       [tableContent removeObjectAtIndex:x];
                 }
            }
       }
       NSLog(@"tableContent count = %@",[tableContent count]);
       [self.tableView reloadData];
}

When i'm calling this method, it gives me Exc_Bad_Access on NSLog(@"tableContent count ...

I think that [tableContent removeAllObjects]; is releasing the array, but it's unreasonable.

Any help will be appreciated.

Upvotes: 0

Views: 400

Answers (2)

David.Chu.ca
David.Chu.ca

Reputation: 38664

In addition to change your NSLog for count with %d, I think the following codes are better than removing contents within your loop:

...
} else {
  NSPredicate* predicate = [NSPredicate predicateWithFormat:
    @"price < %d AND price > %d", _minValue, _maxValue];
  NSArray* array = [tableContent filteredArrayUsingPredicate:predicate];
  tableContent = array;
}
...

Upvotes: 0

Alladinian
Alladinian

Reputation: 35646

count returns an int, so change your NSLog to:

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

and you'll be fine.

Upvotes: 3

Related Questions