NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: 发送到不可变对象的突变方法



我也在执行以下代码将词典从我的末端添加到字典后,将字典从一个ViewController发送到另一个。

 else if ([segue.identifier isEqualToString:@"detail"]) {
        NSString *identifier=segue.identifier;
        NSIndexPath *index = [self.listTableView indexPathForSelectedRow];
        DetailViewController *detailViewController = [segue destinationViewController];
        NSMutableDictionary *selectedDic;
        selectedDic=[[NSMutableDictionary alloc]init];
        selectedDic= [dataArray objectAtIndex:index.row];
        [selectedDic setObject:@"abc" forKey:@"UserID"];
        detailViewController.tripDetails =[selectedDic mutableCopy];
         detailViewController.identifier = identifier;
    }

,但它显示了上述例外。它是什么意思,如何修复?

此错误意味着您正在尝试更改一个不可变形的字典。您将SelectedDIC初始化为NSMutableDictionary,但是您将其重新分配为[DataArray ObjectAtexex:index.row]。您没有发布足够的信息以使我能够确定数据阵列中的内容,但是假设数据阵列是nsdictionaries的数组,则可以通过将该行更改为[[dataarray objectexindex:index.row] mutableCopy]来完成此工作。如果该数据阵列不是一系列字典,那么您可能需要重新考虑您要做的事情。

最新更新