Pregunta

I'm having an issue where I use nothing but NSMutableDictionaries but somewhere in saving/pulling the data from Core Data it's turning into an NSDictionary that I can't edit. Here is the code:

View Controller 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];

I then pull the CD entity in a new view controller based on a set attribute...

ViewControllerB

@property NSMutableDictionary *myDictionary;
@synthesize myDictionary;
myDictionary = [NSMutableDictionary new];

APPMyObject *details = [APPMyObject findFirstByAttribute:@"myName" withValue:myName];
myDictionary = details.coreDictionary;

The warning occurs on the very last line, "Incompatible pointer types assigning to 'NSMutableDictionary' from 'NSDictionary'. Everything compiles and runs fine, but the warning is there for a reason.

Forgive me if this is fundamental knowledge, but why does saving the dictionary to Core Data change it from mutable to immutable? If I change everything to NSDictionary it works fine, but then I can't edit the information and rewrite.

Thank you!

¿Fue útil?

Solución

In these case, you can use this instead of myDictionary = details.coreDictionary;.

myDictionary = [NSMutableDicationary dictionaryWithDictionary:details.coreDictionary];

Updation: I saw that myDictionary = [NSMutableDictionary new];, this line totally waste. Even you allocate to NSMutableDictionary, during assign with details.coreDictionary, It will try to assign a NSDictionary object with NSMutableDictionary variable which means, myDictionary try to point NSMutableDictionary memory which leads to compile time error. Also Type casting to NSMutableDictionarymyDictionary = (NSMutableDictionary*)details.coreDictionary;, temporary solve your problem but type casting is not correct one. After assign it, when you try to access NSMutable method, it will get crash.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top