Reputation: 19655
I have an NSMutableDictionary
which was created from a JSON string.
The problem is that I need to do some pretty complex data comparisons between a NSArray
and a NSMutableDictionary
. I cannot figure out an efficient way to do so.
The dictionary looks like this:
dictionaryMain
0 = {
key1 = value1;
key2 = value2;
key3 = {
subKey1 = {
subSubKey1 = {
inner1 = apples;
inner2 = oranges;
};
subSubKey2 = {
inner1 = grapes;
inner2 = lemons;
};
subSubKey3 = {
inner1 = mangoes;
inner2 = kiwis;
};
};
};
key4 = {
subKey1 = {
subSubKey1 = {
inner1 = pineapples;
inner2 = apples;
};
subSubKey2 = {
inner1 = oranges;
inner2 = mangoes;
};
};
};
};
1 = {
key1 = value1;
key2 = value2;
key3 = {
subKey1 = {
subSubKey1 = {
inner1 = watermelon;
inner2 = grapes;
};
subSubKey7 = {
inner1 = bananas;
inner2 = oranges;
};
};
};
key4 = {
subKey1 = {
subSubKey5 = {
inner1 = kiwis;
inner2 = mangoes;
};
};
};
};
// ... and so on
I have an array arrayOfKeys
which has some of the subSubKey
s as follows:
arrayOfKeys
{
subSubKey5,
subSubKey7,
subSubKey10
}
I need to loop through my NSMutableDictionary
and select only those objects that have the same keys as defined by arrayOfKeys
. These selected objects will be added to a new dictionary.
For example, if arrayOfKeys
contains subSubKey7
, then the object [1] will be selected from the NSMutableDictionary
since a key deep inside it has the name subSubKey7
.
I tried to figure out a simple way to do this but I couldn't come up with a viable solution. The only thing that I can think of doing is using a lot of nested loops and dirty quick code.
I spent two days trying to figure this out. Any solution for this problem, guys?
Thanks!
Upvotes: 0
Views: 182
Reputation: 119041
You should use a recursive method (or function) which checks the dictionary it is passed and calls itself to process any sub dictionaries. The method should return anything it finds and anything found when it calls itself.
If there should only be one result array (from one level) the method could return as soon as it is found or as soon as the result of calling itself is non-nil.
Upvotes: 2