ios – 在调试器(或日志)中类似NSDictionary的漂亮打印

ios – 在调试器(或日志)中类似NSDictionary的漂亮打印,第1张

概述这一直困扰着我.我如何抵消使用po foo(或通过NSLog)在调试器中转储对象时发生的丑陋转义.我尝试了很多方法来实现-description或-debugDescription无济于事. 鉴于这个简单的课程 @interface Foo : NSObject@property NSDictionary* dict;@end@implementation Foo- (NSString 这一直困扰着我.我如何抵消使用po foo(或通过NSLog)在调试器中转储对象时发生的丑陋转义.我尝试了很多方法来实现-description或-deBUGDescription无济于事.

鉴于这个简单的课程

@interface Foo : NSObject@property NSDictionary* dict;@end@implementation Foo- (Nsstring *)description {    // super.description for the <{classname} pointer> output    return [Nsstring stringWithFormat:@"%@ %@",super.description,self.dict];}@end

并且做作用法

Foo* f0 = [[Foo alloc] init];f0.dict = @{ @"value": @0,@"next": NSNull.null };Foo* f1 = [[Foo alloc] init];f1.dict = @{ @"value": @1,@"next": f0 };Foo* f2 = [[Foo alloc] init];f2.dict = @{ @"value": @2,@"next": f1 };

我们为f0获得了不错的输出

(lldb) po f0<Foo: 0x8cbc410> {    next = "<null>";    value = 0;}

f1的可容忍输出

(lldb) po f1<Foo: 0x8cbc480> {    next = "<Foo: 0x8cbc410> {\n    next = \"<null>\";\n    value = 0;\n}";    value = 1;}

f2的可怕输出

(lldb) po f2<Foo: 0x8cbc4b0> {    next = "<Foo: 0x8cbc480> {\n    next = \"<Foo: 0x8cbc410> {\n    next = \\"<null>\\";\n    value = 0;\n}\";\n    value = 1;\n}";    value = 2;}

调试真实世界对象层次结构时,很难快速解析.我假设自从转储类似嵌套的NSDictionary后,我还缺少其他一些技巧

NSDictionary* d0 = @{ @"value": @0,@"next": NSNull.null };NSDictionary* d1 = @{ @"value": @1,@"next": d0 };NSDictionary* d2 = @{ @"value": @2,@"next": d1 };

保持缩进并避免逃离地狱

(lldb) po d2{    next =     {        next =         {            next = "<null>";            value = 0;        };        value = 1;    };    value = 2;}

UPDATE

切换到-deBUGDescription并简单地转发到字典

@implementation Foo- (Nsstring *)deBUGDescription {    return self.dict.deBUGDescription;}@end

丢失递归输出

(lldb) po f2{    next = "<Foo: 0x8b70e20>";    value = 2;}

内部NSDictionary必须依赖于-description,我在这个例子中没有实现,只有-deBUGDescription.切换到如下所示的内容

@implementation Foo- (Nsstring *)description {    return self.dict.description;}- (Nsstring *)deBUGDescription {    return self.dict.deBUGDescription;}@end

产生类似的坏输出

(lldb) po f2{    next = "{\n    next = \"{\n    next = \\"<null>\\";\n    value = 0;\n}\";\n    value = 1;\n}";    value = 2;}
解决方法 TL; DR;

使用NSContainers-PrettyPrint并小心read docs.

答案很长

经过更多的搜索,我发现了descriptionWithLocale:indent:方法.如上所述,我应该能够在我自己的类中实现它,以实现所需的漂亮打印格式.但是,经过一些失败的尝试后,我找到了一个similar SO question.结果说明了,带有解压缩:缩进:只有在因为“安全问题”而继承基础容器类时才有效.

不满意这种方法,我继续挖掘,发现这个radar,但也是NSContainers-PrettyPrint的解决方案.经过一些试验和错误,我得到了很好的工作. (它不在CocoaPods上,所以你必须手动添加它).

一旦添加了NSContainers-PrettyPrint,你可能也想要JRSwizzle.然后在DEBUG目标预处理器宏中定义DEBUGPRINT_ALL和DEBUGPRINT_SWIZZLE.最后,您可以根据fs_* helpers和best practices实现descriptionWithLocale:indent:方法.

以我的问题中的相同Foo为例

@implementation Foo- (Nsstring*)description{    return [Nsstring stringWithFormat:@"%@ %@",self.dict.description];}- (Nsstring *)descriptionWithLocale:(ID)locale indent:(NSUInteger)level{    Nsstring * indent = [Nsstring fs_stringByFillingWithCharacter:' ' repeated:fspp_spacesPerIndent*level];    NSMutableString * str = [[NSMutableString alloc] init];    [str fs_appendobjectStartWithIndentString:indent caller:self];    [str appendString:[self.dict descriptionWithLocale:locale indent:level+1]];    [str fs_appendobjectEnd];    return str;}@end

给定相同的f0,f1和f2实例将产生以下输出

(lldb) po f0<Foo: 0x8a385c0> {    value = 0;    next = <null>;}(lldb) po f1<Foo: 0x8a38630> {    value = 1;    next = <Foo:0x8a385c0        {            value = 0;            next = <null>;        }>;}(lldb) po f2<Foo: 0x8a38660> {    value = 2;    next = <Foo:0x8a38630        {            value = 1;            next = <Foo:0x8a385c0                {                    value = 0;                    next = <null>;                }>;        }>;}

上面的descriptionWithLocale:indent:可以使用一些调整来减少过多的空格,但它仍然可以替代它们.

总结

以上是内存溢出为你收集整理的ios – 在调试器(或日志)中类似NSDictionary的漂亮打印全部内容,希望文章能够帮你解决ios – 在调试器(或日志)中类似NSDictionary的漂亮打印所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065816.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存