我正在尝试从数组中删除最后两个值。所以,我曾经做下面的代码 -
-(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];
}
}
}
但是,它给出了以下例外 -
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object”
我不知道为什么会这样?我也已经在另一个 viewController 类中使用过这个方法。它在那里工作得很好。但是,这里是例外。