Reputation: 1117
I am parsing a JSON
file and adding each object from the file in an NSDictionary
. Sometimes it may happen that the elements in the file are equal. This means that more than 1 object in my NSDictionary
will have the same bey and object. IF they have the same object, there is no problem, but if they have the same key something very strange happens. they get repeated many times, and its just chaos.
This is how I add objects to my dictionary:
NSArray *messagearray = [data objectForKey:@"message"];
NSArray *namearray = [data objectForKey:@"name"];
CHOrderedDictionary* Dictionary = [CHOrderedDictionary dictionaryWithObjects:namearray forKeys:messagearray];
If instead all the keys are different, everything works just fine. Now, since this might happen... e.g.
{"key":["vfg SJ45FFGJJ76v = 1357429260","v SJ45FFGJJ76v = 1640600269","gh SJ45FFGJJ76v = 330786894","gh SJ45FFGJJ76v = -2045929990","v SJ45FFGJJ76v = -1145652524","d SJ45FFGJJ76v = 1255089702","",""],"object":["G","T","G","G","G","R","",""]}
... 702","",""...
Is there a way to prevent this error? e.g. remove the duplicate key objects?
Upvotes: 1
Views: 2988
Reputation: 1624
An NSDictionary (or subclass) must have unique keys, you can't have duplicates in it.
Saying that, you should take a look to this SO question to remove duplicate before creating your dictionary with the arrays.
Upvotes: 1