Moving NSDictionary to Objective-C

Is there an easy way to pull a specific value from an NSDictionary without calling objectForKey for every level that I want to go down to?

For instance:

 { response = { data = { foo = "bar"; }; user = { "first_name" = "Joe"; "last_name" = "Bloggs"; }; }; } 

Is there an easy way to pull first_name ? In fact, my data is much more nested than that, and I want to pull the data at different levels.

Thanks.

+4
source share
2 answers

Key Value Coding is your friend!

Using KVC, you can do something like:

 NSString * name = [response valueForKeyPath:@"user.first_name"]; 

Here's another related question with answers that may help you , which also points to another question with further development .

Here is the documentation for valueForKeyPath:

+5
source

Use valueForKeyPath ? This is in Apple docs.

This is for an array, but should work similarly: theocacao.com on NSArray and KVC

Also, make sure you use the strings as the key. You may run into problems.

For key encoding reference: Apple Docs for key encoding

+2
source

Source: https://habr.com/ru/post/1388749/


All Articles