从字典访问对象/键值对

时间:2017-06-13 13:30:55

标签: objective-c nsdictionary

我有以下字典

{
"b3e0aded-d57b-4159-9c33-c8b006282334" : {
"57646099-b717-4a2b-b9eb-2935548ae954" : [
  "yes"
]
},
"1b186bc7-52c4-4b87-a97f-cc52054aad24" : {
"aba16653-bda0-4e89-b1c8-63df6faa7c10" : [
  "yes"
]
},
"d765038e-e85a-495d-9932-170852fbd86e" : {
"aba16653-bda0-4e89-b1c8-63df6faa7c10" : [
  "yes"
],
"57646099-b717-4a2b-b9eb-2935548ae954" : [
  "yes"
],
"957bdaba-b23d-4243-8384-62dfa46f0656" : "play"
},
"2a8dd370-2f7e-4c8e-93d5-21102fbc82fd" : {
"aba16653-bda0-4e89-b1c8-63df6faa7c10" : [
  "yes"
]
}
}

如何让每个对象说出来     " b3e0aded-d57b-4159-9c33-c8b006282334" :{     " 57646099-b717-4a2b-b9eb-2935548ae954" :[     "是"    ]    }

OR     " d765038e-e85a-495d-9932-170852fbd86e" :{     " aba16653-bda0-4e89-b1c8-63df6faa7c10" :[     "是"     ]     " 57646099-b717-4a2b-b9eb-2935548ae954" :[     "是"     ]

来自这个NSDictionary?

2 个答案:

答案 0 :(得分:2)

使用每个in循环来获取完整值。即

 let dict = your dictionary

 for each in dict {
     print(each)
 }

答案 1 :(得分:1)

您可以使用objectForKey:来读取密钥的值:

id value = [dict objectForKey:@"b3e0aded-d57b-4159-9c33-c8b006282334"]

使用方括号表示法可以更简洁地表达:

id value = dict[@"b3e0aded-d57b-4159-9c33-c8b006282334"]

如果你想要一个只包含该键的对象映射到它的值,那么最简单的方法就是构造一个带有键和值的新对象:

id key = @"b3e0aded-d57b-4159-9c33-c8b006282334";
id value = dict[key];
id result = @{ key: value }