In my Brain model I have an NSMutableArray property called "stockCars" which holds 8 custom "Car" objects.
In my viewcontroller, I have an action set up that is triggered from 8 different UIButtons.
On the first click of one of the 8 buttons, I blank the Car object that is at the same index of the button that is pressed (using sender tag).
So after one press, I still have 8 Cars in stockCars array but one of them is a fresh alloc/inited (blank) one.
On the second press, I do the same thing and blank a second Car from stockCars.
on the third press a few things happen. First I blank the third Car from the array.
Then I do a bunch of other stuff...
Then I call a class method to replace any blank Cars in my stockCars array with a fresh random "Car" object which puts me back to 8 Cars in my array (no blanks).
Then, I want to start the process over... but for some reason it's giving an error on the 4th press. Here's my code... perhaps someone has an idea??
In my viewController button pressed action I'm doing this...
- (IBAction) addButtonPressed: (UIButton *)sender {
//My UIButtons have tags set 0-7
int senderTag = [sender tag];
[brain.stockIngredients replaceObjectAtIndex:senderTag withObject:[brain getBlankIngredient]];
if (brain.currentSelectedCarSlot == 2) {
...
[brain fillEmptyStock];
brain.currentSelectedCarSlot = 0;
}
else {
brain.currentSelectedCarSlot++;
}
Here's my fillEmptyStock method from Brain.m
- (void)fillEmptyStock
{
NSMutableArray *tempArray = [self.stockCars mutableCopy];
for (Car * car in self.stockCars)
{
if (car.name == nil){
[tempArray replaceObjectAtIndex:[self.stockCars indexOfObject:car] withObject:[self getRandomCar]];
}
}
[self.stockCars release];
self.stockCars = [NSMutableArray arrayWithArray:tempArray];
NSLog(@"sizeof stockCars %i", [self.stockCars count]);
[tempArray release];
}
So yea, on the 4th press I get an error, but its one of those BAD EXCESS ones and it seems to be coming on the line [brain.stockIngredients replaceObjectAtIndex:senderTag withObject:[brain getBlankIngredient]];
Perhaps when I replace an object in an array it gets a new index and then that index is no longer valid or something?? I'm a beginner, thanks.