Print keys of NSDictionary instead of values
21,838
Solution 1
for( NSString *aKey in [dictionary allKeys] )
{
// do something like a log:
NSLog(aKey);
}
OR
NSLog([dictionary allKeys]);
This should do the trick
Solution 2
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.
Borrowing an example from Apple:
NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
NSLog(@"Do something with the key here:%@",key);
}
Comments
-
bruno about 4 years
I download some content from a json webservice.
Is it possible print the keys and not the values instead? For the eventuality of not knowing what the keys are.
-
Eimantas about 12 yearsor just
NSLog(@"%@", [dictionary allKeys])
-
Manuel about 12 yearsOr that, indeed. Depends on what he plans to do with it.
-
bruno about 12 yearsThanks! It worked great! But it wont work if there is an array. For example {"postalcodes":[{"adminCode3":"70805","adminName2":"Politischer Bezirk Reutte","adminName3"}]}