Get all keys of an NSDictionary as an NSArray

Objective CNsarrayNsdictionary

Objective C Problem Overview


Is it possible to get all the keys from a specific NSDictionary as a seperate NSArray?

Objective C Solutions


Solution 1 - Objective C

Just use

NSArray*keys=[dict allKeys];

In general, if you wonder if a specific class has a specific method, look up Apple's own documentation. In this case, see NSDictionary class reference. Go through all the methods. You'll discover many useful methods that way.

Solution 2 - Objective C

Yes it's possible. Use allKeys method:

NSDictionary *yourDictionary;
NSArray * yourKeys
yourKeys = [yourDictionary allKeys];

Solution 3 - Objective C

And if you want to get all keys and values, here's what you do:

for (NSString *key in dictionary) {
    id value = dictionary[key];
    NSLog(@"Value: %@ for key: %@", value, key);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayView Question on Stackoverflow
Solution 1 - Objective CYujiView Answer on Stackoverflow
Solution 2 - Objective CgsempeView Answer on Stackoverflow
Solution 3 - Objective CawfulcodeView Answer on Stackoverflow