Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以创建从plist读入的对象的可变版本_Ios_Objective C_Dictionary_Plist_Immutability - Fatal编程技术网

Ios 是否可以创建从plist读入的对象的可变版本

Ios 是否可以创建从plist读入的对象的可变版本,ios,objective-c,dictionary,plist,immutability,Ios,Objective C,Dictionary,Plist,Immutability,我在我的应用程序中读取一个plist,它的顶层是一个数组。它非常简单,可以确保数组开始时是可变的 self.plistData = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myDataSource" ofType:@"plist"]] mutableCopy]; 在数组的每个元素中都有另一个数组,每个数组都包含一个字典 根据tablecell选择,我正在更改选定索引处字典的属性: [s

我在我的应用程序中读取一个plist,它的顶层是一个数组。它非常简单,可以确保数组开始时是可变的

self.plistData = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myDataSource" ofType:@"plist"]] mutableCopy];
在数组的每个元素中都有另一个数组,每个数组都包含一个字典

根据tablecell选择,我正在更改选定索引处字典的属性:

[self.cellDescriptors[indexPath.section][indexOfTappedRow] setValue:@"YES" forKey: @"isSelected"];
当我试图更改字典值时,我收到了错误
”-[\uu NSCFDictionary removeObjectForKey:]:将mutating方法发送到不可变对象“
。如果plist读入的NSDictionary是不可变的,则该错误是有意义的


有没有办法从plist读入内容并确保任何数组或字典都作为可变版本读入?

最简单的方法是使用
NSPropertyListSerialization
并传递适当的选项以获取可变容器

NSString *path = [[NSBundle mainBundle] pathForResource:@"myDataSource" ofType:@"plist"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSMutableArray *array = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:nil error:nil];
self.plistData = array;
理想情况下,您将使用
error
参数并进行适当的错误检查,但这将让您开始


此代码将加载plist并返回一个可变数组,包含的每个数组和字典也将是可变的。

提供的代码不完整。你在哪里使用-removeObjectForKey?你能展示你如何迭代字典/数组的代码吗?没有内置的“mutableCopy”可以将每个子级对象转换成可变版本
mutableCopy
仅在调用它的顶层执行。@SamB我添加了触发崩溃/错误的代码行