I’m trying to remove last two value from array. So, i used to do below code –
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *resMsg = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSData *responseData = [resMsg dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
if (responseData != nil)
{
array = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
if([array count] == 2) {
}else
{
int k = [self.array count] -2 ;
int l = [self.array count] -1 ;
[self.array removeObjectAtIndex:l];
[self.array removeObjectAtIndex:k];
[gridTable reloadData];
[tblProducts reloadData];
}
}
}
But, its giving below exception –
Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object’
I don’t know why this happened? I’ve already used this method in another viewController class too. It was working fine there. But, here its giving exception.
The object returned from
[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];will return an autoreleasedNSArray. If you want to make it mutable, you’ll have to callNSMutableArray *mutableArray = [array mutableCopy];Your other option here is instead of putting in a 0 for your
options:parameter, putNSJSONReadingMutableContainers. This will solve your problem without creating an extra object.EDIT
In order to fix your new problem:
As a side note, this may cause you to have your array objects in a different order than you expect. You may choose to call this…