xcode NSDictionary 里面的 NSDictionary
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12490970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSDictionary inside NSDictionary
提问by Taskinul Haque
Ok I have a .plist file,
好的,我有一个 .plist 文件,
In which I have an NSDictionary *StoredAddresses
,
其中我有一个NSDictionary *StoredAddresses
,
Inside StoredAddresses
, is another handful of NSDictionary
里面StoredAddresses
,是另一把NSDictionary
I know I can use,
我知道我可以使用,
if ([dictionaryOfAddresses objectForKey:@"StoredAddresses"])
To access the top level NSDictionaries, but how can I search for keys inside the stored addresses?
要访问顶级 NSDictionaries,但如何在存储的地址中搜索键?
回答by Christian Stieber
Well, if I read this correctly, you have dictionaries within dictionaries. This works just like any other object:
好吧,如果我没看错的话,字典中就有字典。这就像任何其他对象一样工作:
NSDictionary* Address=[dictionaryOfAddresses objectForKey:@"Home Address"];
NSString* Street=[Address objectForKey:@"Street"];
You could combine the calls if you want to:
如果你想,你可以组合调用:
NSString* Street=[[dictionaryOfAddresses objectForKey:@"Home Address"] objectForKey:@"Street"];
回答by jlehr
You can use the NSKeyValueCoding
method valueForKeyPath:
to access properties of nested objects. For example, given the following dictionaries...
您可以使用该NSKeyValueCoding
方法valueForKeyPath:
访问嵌套对象的属性。例如,给定以下字典......
NSDictionary *homeAddressDict = @{ @"street" : @"2 Elm St.", @"city" : @"Reston" };
NSDictionary *addressesDict = @{ @"home" : homeAddressDict };
...you can access values of the nested home
dictionary as follows:
...您可以home
按如下方式访问嵌套字典的值:
NSString *street = [addressesDict valueForKeyPath:@"home.street"];
NSString *city = [addressesDict valueForKeyPath:@"home.city"];
This works the same for more deeply nested paths, for example:
对于更深层嵌套的路径,这同样适用,例如:
NSDictionary *contactDict = @{ @"name" : @"Jim Ray", @"addresses" : addressesDict };
NSString *street2 = [contactDict valueForKeyPath:@"addresses.home.street"];
NSString *city2 = [contactDict valueForKeyPath:@"addresses.home.city"];
Note that this will work regardless of whether the objects are instances of NSDictionary
or any custom class that descends from NSObject
, provided that the custom class has properties or instance variables whose names match the keys.
请注意,无论对象是 的实例NSDictionary
还是继承自 的任何自定义类NSObject
,只要自定义类具有名称与键匹配的属性或实例变量,这都将起作用。
So for example, instead of using an NSDictionary
for home address, you could substitute an instance of a custom Address
class that had declared properties, getter methods, or instance variables named streetand city(or instance variables named _streetand _city), and still access it the same way.
因此,例如,NSDictionary
您可以替换Address
已声明属性、getter 方法或名为street和city 的实例变量(或名为_street和_city 的实例变量)的自定义类的实例,而不是使用for 家庭地址,并且仍然可以访问它以同样的方式。
And if the object containing the target property is mutable (for example an instance of NSMutableDictionary
), you can even modify the value using the same mechanism, for example:
如果包含目标属性的对象是可变的(例如 的实例NSMutableDictionary
),您甚至可以使用相同的机制修改该值,例如:
[contactDict setValue:@"Herndon" forKeyPath:@"addresses.home.city"];
回答by Ron Holmes
Another new way of getting subdictionaries that makes some neat code is the following shorthand notation:
另一种获取子字典的新方法可以编写一些简洁的代码,这是以下速记符号:
NSString *street = dictionaryOfAddresses[@"HomeAddress"][@"Street"];
回答by Neo
THis will return the array of the keys inside StoredAddresses
这将返回StoredAddresses中的键数组
yourArray=[[dictionaryOfAddresses objectForKey:@"StoredAddresses"] allKeys];
then you can print the yourArray and see the keys, then after knowing the keys you can access using either
然后您可以打印 yourArray 并查看密钥,然后在知道可以使用任一方法访问的密钥后
dict=[[dictionaryOfAddresses objectForKey:@"StoredAddresses"] objectForKey:[yourArray objectAtIndex:index_for_that_key]];
or
或者
dict=[[dictionaryOfAddresses objectForKey:@"StoredAddresses"] objectForKey:@"key_for_your_dict"];