8

我从 json webservice 下载了一些内容。

是否可以打印键而不是值?万一不知道钥匙是什么。

4

2 回答 2

33
for( NSString *aKey in [dictionary allKeys] )
{
    // do something like a log:
    NSLog(aKey);
}

或者

NSLog([dictionary allKeys]);

这应该可以解决问题

于 2012-04-10T14:49:17.607 回答
1

您应该能够使用 NSDictionary 的 keyEnumerator 方法来获取密钥,然后您可以遍历它们并将它们打印出来。

借用苹果的一个例子:

NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;

while ((key = [enumerator nextObject])) {
    NSLog(@"Do something with the key here:%@",key);
}
于 2012-04-10T14:51:45.143 回答