【发布时间】:2014-01-08 05:04:30
【问题描述】:
我遇到了一个问题,我只使用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 实体拉到新的视图控制器中...
视图控制器B
@property NSMutableDictionary *myDictionary;
@synthesize myDictionary;
myDictionary = [NSMutableDictionary new];
APPMyObject *details = [APPMyObject findFirstByAttribute:@"myName" withValue:myName];
myDictionary = details.coreDictionary;
警告出现在最后一行,“不兼容的指针类型从 'NSDictionary' 分配给 'NSMutableDictionary'。一切都编译并运行良好,但警告存在原因。
如果这是基础知识,请原谅我,但是为什么将字典保存到 Core Data 会将其从可变更改为不可变?如果我将所有内容都更改为NSDictionary,它可以正常工作,但是我无法编辑信息并重写。
谢谢!
【问题讨论】:
-
NSMutableDictionary *myDictionary;; myDictionary=[myDictionary initWithDictionary:details.coreDictionary]
-
myDictionary = (NSMutableDictionary*)details.coreDictionary;使用它会处理警告
标签: ios core-data nsdictionary nsmutabledictionary