ios 美化 NSArray 和 NSDictionary 的 NSLog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7121120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:11:08  来源:igfitidea点击:

Beautify NSLog of NSArray & NSDictionary

iosnsarraynsdictionary

提问by iOSDevil

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

我正在处理深度嵌套的 NSArray 和 NSDictionary,至少可以说非常耗时。[data objectatindex:0] valueForKey:@"blah"] 等等

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

有谁知道一个很好的 iOS 类别来递归记录结构、突出显示类型并显示值?

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

可能问得有点多,但你永远不知道:)

回答by Andrey Starodubtsev

Hmm. Simple

唔。简单的

NSLog( @"%@", dictionaryYouWantToPrint );

outputs following result for me:

为我输出以下结果:

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

回答by Gotschi

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)

但除了将其复制并粘贴到jsonFormatter 中(例如)之外,我想不出任何让输出变得美观的好方法

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

编辑:@Andrey Starodubtsev 有以下 XCode > 5.x 的解决方案:

NSLog( @"%@", dictionaryYouWantToPrint );

回答by iOSDevil

Maybe you can use block after iOS5, like

也许你可以在 iOS5 之后使用 block,比如

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

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

回答by Martin Berger

This will print in console without NSLog.

这将在没有 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

在此处输入图片说明