views:

188

answers:

2

I have a dictionary of dictionaries and I have retrieved from a plist.

Suppose the high hierarchy dictionary is COUNTRIES and each sub dictionary is a COUNTRY.

Each COUNTRY sub dictionary has keys like name, currency, population, flag, etc.

After retrieving the dictionary and its subs to a variable as in

NSMutableDictionary * countries = [NSMutableDictionary dictionaryWithContentsOfFile:path];

How do I check to see if a currency named "euros" is present there?

I mean, I am checking for a value in a key of a subdictionary... is it possible? How?

thanks in advance.

NOTE: It is a dictionary inside a dictionary, not an array inside a dictionary or a dictionary inside an array. Each sub dictionary is store inside the main dictionary using a number. So, inside sub dictionary #1 may be a dictionary which keys are, for example, France, euros, 30 million people.

I need to see if France sub dictionary is present inside the main dictionary.

+1  A: 

Use this to get any countries with a currency named "Euro" (in an NSArray).

NSArray *allCountries = [countries allValues];
NSPredicate *euroPredicate = [NSPredicate predicateWithFormat:@"currency == %@", @"Euro"];
NSArray *filteredCountries = [allCountries filteredArrayWithPredicate:euroPredicate];

If all you need to do is check whether such a country exists, then you can just do a simple if (filteredCountries.count > 0) { }.

Sidenote: I don't see a reason for the top data structure to be an NSDictionary if the country name is also stored in the inner NSDictionary (presumably the country name is the outer dictionary key) - perhaps an NSArray as the outer structure would be more appropriate?

Sbrocket
Sbrocket is right (see bext answer). It is exactly what I have. I have added a note to the question.
Digital Robot
Is your question answered then? This snippet should do what you need.
Sbrocket
yes. THANKS!!!!!
Digital Robot
+1  A: 
NSDictionary *_currencyDict = [countries objectForKey:@"currency"];
NSArray *_currencies = [_currencyDict allValues]; 
NSPredicate *_currencyPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", _currencies];
BOOL _eurosResult = [_currencyPredicate evaluateWithObject:@"euros"];

if (_eurosResult)
   NSLog(@"The value 'euros' is in the 'currency' dictionary, within the 'countries' dictionary.");
else
   NSLog(@"The value 'euros' was not found.");
Alex Reynolds
The countries dictionary wouldn't contain an object with key @"currency" according to his question description. I suggest you re-read it.
Sbrocket
Actually, he says the opposite: "Each COUNTRY sub dictionary has keys like name, currency, population, flag, etc." The values associated with those keys are `NSDictionary` objects, if I'm reading correctly.
Alex Reynolds
Re-reading it, I'm not sure what the structure is of his data. I'll leave my answer until there's a clarification -- either way, your answer or mine could be used to solve the problem.
Alex Reynolds
I interpret it as this: http://gist.github.com/241593
Sbrocket
Sbrocket is right. It is exactly what I have. I have added a note to the question.
Digital Robot
Sounds good, thanks for clarifying.
Alex Reynolds