Beautify NSLog of NSArray & NSDictionary

…衆ロ難τιáo~ 提交于 2019-12-02 16:35:06

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 );

Hmm. Simple

NSLog( @"%@", dictionaryYouWantToPrint );

outputs following result for me:

{
    id = 1;
    matchCount = 0;
    matchPattern = abcdef;
    number = "123456";
    sessionID = 5;
    status = Unknown;
    timerStart = 1367229348;
}

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);
}];

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!