46

I'm dealing with deeply nested NSArray's and NSDictionary's and it's very time consuming to say the least. [data objectatindex:0] valueForKey:@"blah"] etc etc

Does anyone know of a nice iOS category to recursively log the structure, highlight the type and show the values?

Might be asking a bit much but you never know :)

durron597
  • 31,968
  • 17
  • 99
  • 158
iOSDevil
  • 1,786
  • 3
  • 16
  • 29

4 Answers4

136

Hmm. Simple

NSLog( @"%@", dictionaryYouWantToPrint );

outputs following result for me:

{
    id = 1;
    matchCount = 0;
    matchPattern = abcdef;
    number = "123456";
    sessionID = 5;
    status = Unknown;
    timerStart = 1367229348;
}
Andrey Starodubtsev
  • 5,139
  • 3
  • 32
  • 46
57

Maybe like this?

for (id key in dictionary) {
    NSLog(@"key: %@, value: %@ \n", key, [dictionary objectForKey:key]);
}

but i can't think of any nice way of getting the output beautiful except copy & paste it into a jsonFormatter (for example)

EDIT: @Andrey Starodubtsev has the solution for XCode > 5.x below:

NSLog( @"%@", dictionaryYouWantToPrint );
Gotschi
  • 3,175
  • 2
  • 24
  • 22
  • 1
    Care to describe how that (passing the object to `NSLog()` with the `%@` specifier) works? – Droppy Jun 15 '16 at 11:56
  • @Droppy you mean this question? http://stackoverflow.com/questions/2634099/basic-objective-c-syntax – Gotschi Jun 20 '16 at 09:19
1

Maybe you can use block after iOS5, like

[anArray enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
    NSLog (@"object->%@",object);
}];

[aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop){
    NSLog(@"key->%@, value-> %@",key,object);
}];
1

This will print in console without NSLog.

During debugging, when your breakpoint is below your dictionary, you can type in console

NSDictionary * myDict = ...;

po myDict

and you will get printed dictionary in console.

You can even cast objects to another types in console:

enter image description here

Martin Berger
  • 1,639
  • 3
  • 18
  • 39