我有一个NSMutableDictionary
初始化如下:
dictionary = [NSMutableDictionary dictionaryWithDictionary:@{
@"0": @{
@"title": @"Description",
@"value": @"Enter Description",
},
@"1": @{
@"title": @"Amount",
@"value": @"Enter Amount",
}
}];
现在我想修改@"Enter Description"
我一直在尝试这样做:
NSMutableDictionary *tempDictionary = [dictionary objectForKey:@"0"];
[tempDictionary setObject:@"TEST" forKey:@"value"];
//The next line doesn't get executed yet since the error occurs at the line above.
//So Maybe there is something wrong with the one below as well ;-)
[dictionary setObject:tempDictionary forKey:@"0"];
但是我收到以下错误消息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance
我发现不知何故我的NSMutableDictionary
一定已经变成了NSDictionary
,这就是为什么我无法编辑它。也许您可以帮我解决这个问题,并为我指明正确的方向,或者向我展示可以找到答案的资源?
最佳答案
您的“子词典”是不可变的。
试试这个:
dictionary = [NSMutableDictionary dictionaryWithDictionary:@{
@"0": [NSMutableDictionary dictionaryWithDictionary:@{
@"title": @"Description",
@"value": @"Enter Description",
}],
@"1": [NSMutableDictionary dictionaryWithDictionary:@{
@"title": @"Amount",
@"value": @"Enter Amount",
}]
}];
关于objective-c - NSMutableDictionary 中的编辑条目失败,错误为 : "unrecognized selector sent to instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32105650/