崩溃:发送到不可变对象的变异方法

时间:2014-02-24 13:52:04

标签: ios iphone objective-c ipad nsdictionary

我正在尝试删除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方法'

有什么想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

你制作了一个可变数组的副本,但是数组中的字典仍然是不可变的。

你需要可变容器。

替换

NSArray *allWebDictionary = [NSJSONSerialization JSONObjectWithData:webData
                                                            options:0
                                                              error:nil];

NSMutableArray *allWebDictionary = [NSJSONSerialization JSONObjectWithData:webData
                                                                   options:NSJSONReadingMutableContainers
                                                                     error:nil];