Search code examples
iosobjective-cnsarraynsmutabledictionary

Easiest way to turn NSMutableDictionary into an NSArray


I have an NSMutableDictionary that is finished loading it's data. The dictionary has data like so:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

The data is just NSStrings. So like this would be an example:

dict[@"a"] = @"a";
dict[@"b"] = @"b";
dict[@"c"] = @"c";

I don't know what the data will be till the loop that generates it is run (but it will be like the pattern above).

What's the best/easiest way to turn this into an NSArray?


Solution

  • If you want to convert it into an NSArray only keeping its keys, you should use

    NSArray *keysArray =  [dict allKeys];
    

    If you want values, you can use

    NSArray *valuesArray = [dict allValues];
    

    If you want both, you can add them.