Aniruddh
Aniruddh

Reputation: 7668

Creating an Array of values from an Array of Dictionaries

The question sounds weird but I'm getting an array of dictionaries as parsed result.

Something like this:

parsed content: (
        {
        "name" = "John";
        "lastname" = "Doe";
        "foo" = "bar";
    }

What would be the suggestion for best way to create an array of values??

Upvotes: 0

Views: 130

Answers (3)

danh
danh

Reputation: 62676

Like this?

- (void)flattenDictionary:(NSDictionary *)d intoKeys:(NSMutableArray *)keys andValues:(NSMutableArray *)values {

    for (id key in [d allKeys]) {
        [keys addObject:key];
        [values addObject:[d valueForKey:key]];
    }
}

- (void)flattenDictionaries:(NSArray *)arrayOfDictionaries {

    NSMutableArray *keys = [NSMutableArray array];
    NSMutableArray *values = [NSMutableArray array];

    for (NSDictionary *d in arrayOfDictionaries) {
        [self flattenDictionary intoKeys:keys andValues:values];
    }

    NSLog(@"now we have these values %@", values);
    NSLog(@"corresponding to these keys %@", keys);
}

Upvotes: 3

Abhi Beckert
Abhi Beckert

Reputation: 33379

You can get the values with:

NSArray *values = dictionary.allValues;

Or, loop through it:

[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
    NSLog(@"%@ = %@", key, object);
}];

Upvotes: 2

virindh
virindh

Reputation: 3765

To do that loop through them and create an array.

Upvotes: 1

Related Questions