Law*_*ton 1 logging cocoa objective-c pretty-print
NSArray的-description方法将嵌套递归调用,如下所示:
2009-05-15 14:28:09.998 TestGUIProject[29695:813] (
a, // Array1 item 1
( // Array2, a second array, nicely indented another 4 spaces
a // Item in Array2
) // End of Array2
) // End of Array1
Run Code Online (Sandbox Code Playgroud)
我想为我自己的自定义类做一些类似的事情(使用我正在编写的脚本).
我不知道的是,当递归调用的对象添加自己的新行时,如何添加额外的缩进级别.
我所拥有的是以下内容:
- (NSString *)description {
return [NSString stringWithFormat:@"{{{\n"
@" prop1: %@\n"
@" prop2: %@\n"
@" prop3: %@\n"
@" prop4: %@\n"
@"}}}",
self.prop1,
self.prop2,
self.prop3,
self.prop4];
}
Run Code Online (Sandbox Code Playgroud)
但是,只要其中一个属性是NSArray或使用相同描述格式的另一个对象,它就会崩溃,因为它不能很好地嵌套.
相反,你得到:
2009-05-15 14:25:50.899 TestApp[29636:813] {{{
prop1: SomeValue1
prop2: ( // Prop 2 is an Array of strings
"String1", // Note no additional level of indentation as in the NSArray example
"String2",
"String3",
"String4"
)
prop3: SomeValue3
prop4: SomeValue4
}}}
Run Code Online (Sandbox Code Playgroud)
我如何获得额外的嵌套级别?
你想要的是这个函数,在NSArray和NSDictionary上可用:
- (NSString *) descriptionWithLocale: (id) locale indent: (NSUInteger) level;
Run Code Online (Sandbox Code Playgroud)
指定缩进值1以使嵌套数组或字典按指定的量缩进它打印的所有内容.