j.TK
j.TK

Reputation: 223

access Object/Key-value pair from dictionary

I have the following dictionary

{
"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"
]
}
}

How to get each object say "b3e0aded-d57b-4159-9c33-c8b006282334" : { "57646099-b717-4a2b-b9eb-2935548ae954" : [ "yes" ] }

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

from this NSDictionary?

Upvotes: 0

Views: 250

Answers (2)

Tanvir Nayem
Tanvir Nayem

Reputation: 722

use for each in loop to get the full value. i.e

 let dict = your dictionary

 for each in dict {
     print(each)
 }

Upvotes: 2

Julian Goacher
Julian Goacher

Reputation: 567

You can use objectForKey: to read the value for a key:

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

Which can be expressed more concisely using square bracket notation:

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

If you then want an object containing just that key mapped to its value then the easiest thing to do is just to construct a new object with the key and value:

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

Upvotes: 1

Related Questions