I am learning Objecive-C and try to removeObject method in loop.
It is not work correctly. I recive “Thread 1: Program received signal:”SIGABRT”.” on that line.
What is wrong?
NSMutableArray *bookListMutable = [NSMutableArray arrayWithObjects:@"TitleM 2", @"TitleM 4", @"TitleM 5", nil];
[bookListMutable addObject:@"TitleM 3 added"];
[bookListMutable insertObject:@"TitleM 1 inserted" atIndex:0];
long countMutable;
countMutable = bookListMutable.count;
for(int y = 0; y < countMutable; y++){
[bookListMutable removeObject:[bookListMutable objectAtIndex:y]];
}
P.S. xCode 4.1
You should not remove items in that order. Let’s say the array has 4 items
{0, 1, 2, 3}and simulate the execution of the program:First iteration with
y = 0: removing item at index0from{0, 1, 2, 3}gives{1, 2, 3}.Second iteration with
y = 1: removing item at index1from{1, 2, 3}gives{1, 3}Third iteration with
y = 2: removing item at index2from{1, 3}gives. Oops, there is no item at index 2 => Exception.So you should iterate backwards like this:
or use the build in method: