TL;DR;
Use NSContainers-PrettyPrint and carefully read the docs.
Long Answer
After much more searching I discovered the descriptionWithLocale:indent:
method. As documented, I should've been able to implement this in my own classes to achieve the desired pretty-print formatting. However, after some failed attempts I found a similar SO question. Turns out descriptionWithLocale:indent:
only works if you subclass a Foundation container class because of "security concerns".
Unsatisfied with that approach I continued digging and found this radar but also a solution in NSContainers-PrettyPrint. After some trial and error I got things working decently well. (It's not on CocoaPods so you have to add it manually).
Once NSContainers-PrettyPrint is added you'll probably want JRSwizzle too. Then define DEBUGPRINT_ALL
and DEBUGPRINT_SWIZZLE
in your DEBUG targets preprocessor macros. Finally, you can implement your descriptionWithLocale:indent:
methods in terms of the fs_* helpers and best practices.
Using the same Foo
from my question as an example
@implementation Foo
- (NSString*)description
{
return [NSString stringWithFormat:@"%@ %@", super.description, 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
Would produce the following output given the same f0
, f1
and f2
instances
(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>;
}>;
}>;
}
The above descriptionWithLocale:indent:
could use some tweaking to reduce the excessive whitespace but it still beats the alternatives.