Search code examples
objective-c

How to get array of values from array of dictionary?


NSArray *arrTemp = @[
                     @{@"Appearance" : @"Copper",
                       @"Bitterness" : @(50),
                       @"Style" : @"India Pale Ale (IPA)"},

                     @{@"Appearance" : @"Jet Black",
                       @"Bitterness" : @(35),
                       @"Style" : @"Stout"},

                     @{@"Appearance" : @"Copper",
                       @"Bitterness" : @(25),
                       @"Style" : @"English Brown Ale"},

                     @{@"Appearance" : @"Deep Gold",
                       @"Bitterness" : @(25),
                       @"Style" : @"Bock"}
                     ];

From the above structure, how can we get the array of "Appearance" values. Please help.


Solution

  • Key Value Coding is your friend. See the paragraph Getting Attribute Values Using Keys

    NSArray *appearances = [arrTemp valueForKey:@"Appearance"];
    

    valueForKey called on an array of dictionaries returns an array of all values for the specific key.