我正在尝试删除NSMutableDictionary
中特定键的对象,但是在尝试执行此操作时应用程序崩溃了。这是我的代码:
NSArray *allWebDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
self.results = [[NSMutableArray alloc] initWithArray:allWebDictionary];
for (NSMutableDictionary *dic2 in self.results) {
NSMutableArray *incidents = [dic2 valueForKey:@"incidents"];
for (NSMutableDictionary *incident in incidents) {
[incident removeObjectForKey:@"type"];
}
}
我的应用程序崩溃了以下日志:
* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:' - [__ NSCFDictionary removeObjectForKey:]:发送到不可变对象的mutating方法'
有什么想法吗?提前谢谢!
答案 0 :(得分:0)
你制作了一个可变数组的副本,但是数组中的字典仍然是不可变的。
你需要可变容器。
替换
NSArray *allWebDictionary = [NSJSONSerialization JSONObjectWithData:webData
options:0
error:nil];
与
NSMutableArray *allWebDictionary = [NSJSONSerialization JSONObjectWithData:webData
options:NSJSONReadingMutableContainers
error:nil];