Antonio MG
Antonio MG

Reputation: 20410

NSDictionary to NSArray containing keys and values

I don't know if there is an easy way to implement this, or I need to implemente the algorithm by myself.

This is what I have (NSDictionary):

 {
    14 =         (
        "M"
    );
    15 =         (
        "H"
    );
    16 =         (
        "F",
        "G"
    );

And this is what I want (NSArray):

     {
        14,
        "M",
        15,
        "H"
...
}

Im looking for an easy way, in case I need to do the algorithm I'll do by myself

Upvotes: 1

Views: 2255

Answers (2)

Pfitz
Pfitz

Reputation: 7344

You could iterate over the NSDictionaryand add the keys and values like this:

  NSMutableArray *array = [NSMutableArray array];
    [may enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
    {
        [array addObject:key];

        for (int i = 0; i < [obj count]; i++){
            [array addObject:[obj objectAtIndex:i]];
        }
    }];

Upvotes: 3

beryllium
beryllium

Reputation: 29767

Try this way:

NSMutableArray *allObjs = [[NSMutableArray alloc] initWithArray:[d allKeys ]];
[allObjs addObjectsFromArray:[d allValues]];

Upvotes: 1

Related Questions