NSDictionary inside NSDictionary

Ok I have a .plist file,

In which I have NSDictionary *StoredAddresses,

Inside StoredAddressesthere is another partNSDictionary

I know I can use

if ([dictionaryOfAddresses objectForKey:@"StoredAddresses"])

To access NSD top-level dictionaries, but how can I search for keys inside stored addresses?

+5
source share
4 answers

Well, if I read it correctly, you have dictionaries in the dictionaries. This works the same as any other object:

NSDictionary* Address=[dictionaryOfAddresses objectForKey:@"Home Address"];
NSString* Street=[Address objectForKey:@"Street"];

You can combine calls if you want:

NSString* Street=[[dictionaryOfAddresses objectForKey:@"Home Address"] objectForKey:@"Street"];
+10
source

You can use the method NSKeyValueCoding valueForKeyPath:to access the properties of nested objects. For example, given the following dictionaries ...

NSDictionary *homeAddressDict = @{ @"street" : @"2 Elm St.", @"city" : @"Reston" };
NSDictionary *addressesDict = @{ @"home" : homeAddressDict };

... home :

NSString *street = [addressesDict valueForKeyPath:@"home.street"];
NSString *city = [addressesDict valueForKeyPath:@"home.city"];

, :

NSDictionary *contactDict = @{ @"name" : @"Jim Ray", @"addresses" : addressesDict };

NSString *street2 = [contactDict valueForKeyPath:@"addresses.home.street"];
NSString *city2 = [contactDict valueForKeyPath:@"addresses.home.city"];

, , NSDictionary , NSObject, , , .

, , NSDictionary Address, , getter street ( _street _city), - .

, , (, NSMutableDictionary), , :

[contactDict setValue:@"Herndon" forKeyPath:@"addresses.home.city"];
+2

, , - :

NSString *street = dictionaryOfAddresses[@"HomeAddress"][@"Street"];
+1

StoredAddresses

yourArray=[[dictionaryOfAddresses objectForKey:@"StoredAddresses"] allKeys];

, , ,

dict=[[dictionaryOfAddresses objectForKey:@"StoredAddresses"] objectForKey:[yourArray objectAtIndex:index_for_that_key]];

dict=[[dictionaryOfAddresses objectForKey:@"StoredAddresses"] objectForKey:@"key_for_your_dict"];
0
source

All Articles