我的数组结构如下:
NSMutableArray topArray{
NSMutableArray middleArray{
NSMutableArray lowerArray{
NSMutableDictionary dict{
}
}
}
}
数组结构填充了我从网络检索的 JSON 格式的一些数据。
我正在尝试编辑 NSMutableDictionary 中的对象之一,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *lowerArray = [[self middleArray] objectAtIndex:[indexPath section]];
NSMutableDictionary *dict = [lowerArray objectAtIndex:[indexPath row]];
[dict setObject:[NSNumber numberWithInt:1] forKey:@"key"];
[[self tableView] reloadData];
}
每个数组中的数据都是正确的(我已经使用 print 语句进行了检查),但是,当我尝试更改字典中的对象时,出现以下错误: 原因:
'-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
我需要在数组结构中更改字典中的对象。
这可能是 JSON 数据的问题吗,因为当 topArray 首次使用 JSON 数据初始化时,中间和较低的数组只是 NSArray 的形式?
抱歉,如果这令人困惑,如果您有任何疑问,我会尽力澄清更多信息。
预先感谢您的帮助。
最佳答案
如果您使用的是 NSJSONSerialization
,则可以将 NSJSONReadingMutableContainers
传递给 +JSONObjectWithData:options:error 的
,并且所有解析的字典和数组都是可变的。options
参数:
NSMutableArray *topArray = [NSJSONSerialization JSONObjectWithData:webServiceData
options:NSJSONReadingMutableContainers
error:nil];
关于ios - 替换数组中字典中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22797075/