ios 打印 NSDictionary 的键而不是值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10090950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print keys of NSDictionary instead of values
提问by bruno
I download some content from a json webservice.
我从 json 网络服务下载了一些内容。
Is it possible print the keys and not the values instead? For the eventuality of not knowing what the keys are.
是否可以打印键而不是值?对于不知道密钥是什么的可能性。
回答by Manuel
for( NSString *aKey in [dictionary allKeys] )
{
// do something like a log:
NSLog(aKey);
}
OR
或者
NSLog([dictionary allKeys]);
This should do the trick
这应该可以解决问题
回答by wtbgtr
You should be able to use the NSDictionary's keyEnumerator method to get the keys, then you can loop through them and print them out.
您应该能够使用 NSDictionary 的 keyEnumerator 方法来获取密钥,然后您可以遍历它们并将它们打印出来。
Borrowing an example from Apple:
借用苹果的例子:
NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
NSLog(@"Do something with the key here:%@",key);
}