我遇到的问题是除了NSMutableDictionaries
之外我什么都没用,但在保存/拉取Core Data
数据的某个地方,它变成了我无法编辑的NSDictionary
。这是代码:
查看控制器A
@property NSMutableDictionary *myDictionary;
@synthesize myDictionary;
myDictionary = [NSMutableDictionary new];
//Pull dictionary data in IBAction from somewhere else
APPOtherViewController *source = [segue sourceViewController];
myDictionary = source.otherDictionary; //other dictionary is also NSMutableDictionary
//On a button press, I save this dictionary to a Core Data transformable attribute (using MR_shorthand):
APPMyObject *newObject = [APPMyObject createEntity];
newObject.coreDictionary = myDictionary;
[[NSManagedObjectContext defaultContext]saveToPersistentStoreAndWait];
然后我根据设置属性...
在新的视图控制器中拉出CD实体ViewControllerB
@property NSMutableDictionary *myDictionary;
@synthesize myDictionary;
myDictionary = [NSMutableDictionary new];
APPMyObject *details = [APPMyObject findFirstByAttribute:@"myName" withValue:myName];
myDictionary = details.coreDictionary;
警告发生在最后一行,“不兼容的指针类型从'NSMutableDictionary
'分配给'NSDictionary
'。所有内容编译并运行良好,但警告是有原因的。
请原谅我,如果这是基础知识,但为什么保存字典Core Data
将其从可变变为不可变?如果我将所有内容更改为NSDictionary
它可以正常工作,但我无法编辑信息并重写。
谢谢!
答案 0 :(得分:4)
在这种情况下,您可以使用此代替myDictionary = details.coreDictionary;
。
myDictionary = [NSMutableDicationary dictionaryWithDictionary:details.coreDictionary];
<强>更新用:强>
我看到myDictionary = [NSMutableDictionary new];
,这条线完全浪费了。即使您在使用details.coreDictionary
分配期间分配给NSMutableDictionary,它也会尝试使用NSDictionary
变量分配NSMutableDictionary
对象,这意味着myDictionary
尝试指向NSMutableDictionary
内存导致编译时错误。同时键入强制转换为NSMutableDictionary myDictionary = (NSMutableDictionary*)details.coreDictionary;
,临时解决您的问题,但类型转换是不正确的。在分配之后,当您尝试访问NSMutable方法时,它将崩溃。