I am using the following code to check whether an object exists or not before removing it if it does:
if(titlescopy.count >= i)
{
if([[titlescopy objectAtIndex:i] isKindOfClass:[NSString class]])
{
[titlescopy removeObjectAtIndex:i];
}
}
However, I am getting this error:
* Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 ..
1]’
* First throw call stack: (0x365a56c3 0x3881e97f 0x364f1055 0x494b 0x37fdb8d5 0x37fe7d75 0x37fe7a81 0x38001ddd 0x38001b97 0x172bd
0x3805f8e5 0x3805f897 0x3805f875 0x3805f12b 0x3805f621 0x37f87d29
0x37f74f29 0x37f74843 0x34ea25d3 0x34ea2203 0x3657a593 0x3657a537
0x365793b9 0x364ec39d 0x364ec229 0x34ea131b 0x37fc88f9 0x283b 0x2798)
libc++abi.dylib: terminate called throwing an exception (lldb)
Why is this happening? The code I’m using should check to see if the object exists or not before trying to remove it and it clearly isn’t.
Thanks!
Array indexing in Objective-C starts at 0, so i = titlescopy.count is out of bounds. Change your if statement to: