定食屋おろポン

おろしポン酢と青ネギはかけ放題です

RecursiveDescriptionについて

UIKitのUIViewクラスには、ビューのヒエラルキー再帰的にダンプする -(NSString*)recursiveDescription というプライベートAPIが用意されています。
しかし、どうもこれはAppKitのNSViewクラスには定義されていないようです。
ないので書きました。UIViewのrecursiveDescriptionとまったく同じ挙動をするはずです。

#if TARGET_OS_IPHONE
@interface UIView (RecursiveDescription)
#else
@interface NSView (RecursiveDescription)
#endif
#ifdef DEBUG
- (NSString*)_recursiveDescription;
#endif
@end
 
#if TARGET_OS_IPHONE
@implementation UIView (RecursiveDescription)
#else
@implementation NSView (RecursiveDescription)
#endif
 
- (NSString*)_recursiveDescription
{
    return _recursiveDescription(self, 0);
}
 
NSString* _recursiveDescription(id view, NSUInteger depth)
{
    NSMutableString* subviewsDescription;
    subviewsDescription = [NSMutableString string];
    for (id v in [view subviews]) {
        [subviewsDescription appendString:_recursiveDescription(v, depth+1)];
    }
    
    NSMutableString* layout;
    layout = [NSMutableString string];
    for (int i = 0; i < depth; i++) {
        [layout appendString:@"   | "];
    }
    
    return [NSString stringWithFormat:@"%@%@\n%@", layout, [view description], subviewsDescription];
}
 
@end

gist: https://gist.github.com/oropon/5159805

DEBUGマクロは別に必要ない気がしてきた。
ついでに一行ブロックバージョンも書いておきました。ちょっと確認したいときにこれ貼り付けて確認して、デバッグできたら消せば良い。

NSString*(^__block __weak r)(id,int)=^(id v,int d){NSMutableString*m,*l;m=[NSMutableString string];l=[NSMutableString string];for(id _ in [v subviews])[m appendString:r(_,d+1)];for(int i=0;i<d;i++)[l appendString:@"   | "];return[NSString stringWithFormat:@"%@%@\n%@",l,[v description], m];};
NSLog(@"\n%@", r(<#target_view#>, 0));

gist: https://gist.github.com/oropon/5160008