以 NSArray 的形式获取 NSDictionary 的所有键

是否有可能从一个特定的 NSDictionary获得所有的键作为一个独立的 NSArray

72407 次浏览

Yes it's possible. Use allKeys method:

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

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.

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);
}