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);
}
Share:
21,838
bruno
Author by

bruno

Mobile Developer

Updated on March 03, 2020

Comments

  • bruno
    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
    Eimantas about 12 years
    or just NSLog(@"%@", [dictionary allKeys])
  • Manuel
    Manuel about 12 years
    Or that, indeed. Depends on what he plans to do with it.
  • bruno
    bruno about 12 years
    Thanks! It worked great! But it wont work if there is an array. For example {"postalcodes":[{"adminCode3":"70805","adminName2":"Politisc‌​her Bezirk Reutte","adminName3"}]}