Get all keys of an NSDictionary as an NSArray

2019-01-31 10:38发布

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

3条回答
老娘就宠你
2楼-- · 2019-01-31 11:18

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.

查看更多
女痞
3楼-- · 2019-01-31 11:23

Yes it's possible. Use allKeys method:

NSDictionary *yourDictionary;
NSArray * yourKeys
yourKeys = [yourDictionary allKeys];
查看更多
等我变得足够好
4楼-- · 2019-01-31 11:35

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);
}
查看更多
登录 后发表回答