Question

In my project I have to load a number of json files. I parse them with JSONKit and after every single parsing with

NSMutableDictionary *json = [myJSON objectFromJSONString]; 

I add them to an array like:

[self.themeArray addObject:json];

This works fine so far. Now I need to pass the dictionaries arround between views. Works so far as well, but I need to add few more objects to the dictionary object-> json. Even it I declared json as NSMutableDictionary it does not allow me to add objects as it seems the JSONKit parser creates non-mutable dictionaries.

I was thinking about creating an object which contains the json dictionary and my additional data side by side so I wouldn´t have to change the json dictionary. I could even change it to NSDictionary because there is no need to change it. But that seems somehow not-elegant to me.

Do you have any idea how I can solve this issue without changing the JSONKit lib?

Thanks in advance!

EDIT

i just tried after changing my code to

NSMutableDictionary *json = [[myJSON objectFromJSONString] mutableCopy];

something like this

[[self.theme objectForKey:@"theme"]  setObject:sender forKey:@"sender"];
[[self.theme objectForKey:@"theme"]  setValue:sender forKey:@"sender"];

Xcode throws an exception:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '* -[JKDictionary setObject:forKey:]: mutating method sent to immutable object'

I assume that´s due to the fact there are still nested dictionaries in the superior dictionary. Then i would have to interate through my json object to copy all dictionaries to mutable dictionaries, right?

Perhaps it's better to switch to NSJSONSerialization as suggested by Guillaume.

EDIT

I just tried something like this

[self.theme  setValue:sender forKey:@"sender"];

And it works now! It was as i assumend. Only the json object was copied to a mutable object. Probably obvious to you, it was not to me.

Thank you all for your help!

EDIT

Finally i changed my code again after i could not manage to change all objects deep inside my dictionary data to mutable objects. I threw out JSONKit and use now NSJSONDeserialization as recommendet here with the option NSJSONReadingMutableContainers. My code looks now like this and all containers (arrays and dictionaries) are mutable deep inside too. That makes me happy! ;-)

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:myJSON options:NSJSONReadingMutableContainers error:&jsonParsingError];
Was it helpful?

Solution

You can always create mutable versions of objects from their non-mutable counterparts by copying them.

NSMutableDictionary* json = [[myJSON objectFromJSONString] mutableCopy];

It is not optimal, but copying smaller dictionaries does is usually not noticable from a performance point of view.

OTHER TIPS

Even it I declared json as NSMutableDictionary it does not allow me to add objects as it seems the JSONKit parser creates non-mutable dictionaries.

What type the variable is declared at means nothing. You could have declared json as NSNumber and that wouldn't make it an NSNumber.

You need to make a mutable copy of the dictionary (with mutableCopy) to get an NSMutableDictionary.

I have three ideas for your.

  1. Create real data model objects and store them in your array. Use the JSON dictionary to init your object.
  2. Store NSMutableDictionary objects in your array. Pass the JSON dictionary to +[NSMutableDictionary dictionaryWithDictionary:] to init the NSMutableDictionary. Others have suggested calling -[NSDictionary mutableCopy] on the JSON dictionary to do the same thing.
  3. Create a category based on NSDictionary that stores the additional data.

NOTES:

Generally creating classes to represent your data is considered the best option, but it is also the most amount of up front work. Basically you are trading more up front work against more maintenance work as you try to keep up maintaining the dictionaries.

Storing mutable dictionary is exactly what you seem to be asking for, but it may be lots of works to find all the places where JSON dictionaries are added to the array and replacing them with the new call.

Creating a category for NSDictionary means you shouldn't need to change any of your current code, but it requires maintainers to understand how you have enhanced NSDictionary. In addition, it will help separate your changes from the original parsed JSON. You can use associated objects to store the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top