ios - 在for循环中将NSDictionary转换为NSMutableDictionary

标签 ios objective-c cocoa-touch nsdictionary nsmutabledictionary

我有以下代码:

for (NSMutableDictionary *aDict in array)
{
     // do stuff
     [aDict setObject:myTitle forKey:@"title"];
}

我的问题是,如果array中充满了NSDictionary对象,编写的for循环代码会自动将其转换为NSMutableDictionary对象吗?

还是我需要在这里做一些更具体的事情以确保在循环中不会在unrecognized selector sent to instance上收到setObject:forKey:错误?

最佳答案

目前,这将给您您提到的错误。尽管使用可变字典设置了循环,但基础对象仍然是不可变的。您需要从中创建一个新词典。尝试这个

NSMutableArray *newArray = [NSMutableArray array];
for (NSDictionary *aDict in array)
{
     NSMutableDictionary *mutable = [aDict mutableCopy];
     // do stuff
     [mutable setObject:myTitle forKey:@"title"];
     [newArray addObject:mutable];
}

关于ios - 在for循环中将NSDictionary转换为NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23659614/

相关文章:

objective-c - 拖动 vom NSTableView 的 NSDragOperation 为 -1

objective-c - ios6 mapkit错误模拟器

objective-c - 核心数据 + NSTextView + 撤消管理器

iOS UITableView reloadData 仅在我第二次调用我的重新加载函数时刷新表格

ios 应用程序在重新打开时崩溃

ios - lldb 无法在导入的 dylib 中查找符号

ios - 在应用程序退出时保存变量

ios - 关闭 ModalViewController 时返回除 ParentViewController 之外的 ViewController

objective-c - UITableViewCell setSelectedBackground 无法正确显示框架

objective-c - 在 iOS 应用程序上保存数据的最有效方式