2010-11-12 10 views

Respuesta

41

No sé exactamente DebugPrintControlHierarchy impreso, pero NSView tiene un método útil llamada _subtreeDescription que devuelve una cadena que describe toda la jerarquía debajo del receptor, incluye clases, marcos y otra información útil.

No tenga miedo del subrayado _. No es una API pública, pero está aprobada para uso público en gdb. Puede verlo mencionado in the AppKit release notes junto con algunos resultados de muestra.

5

Aquí es la tripa de una categoría NSView he construido un tiempo atrás:

+ (NSString *)hierarchicalDescriptionOfView:(NSView *)view 
             level:(NSUInteger)level 
{ 

    // Ready the description string for this level 
    NSMutableString * builtHierarchicalString = [NSMutableString string]; 

    // Build the tab string for the current level's indentation 
    NSMutableString * tabString = [NSMutableString string]; 
    for (NSUInteger i = 0; i <= level; i++) 
    [tabString appendString:@"\t"]; 

    // Get the view's title string if it has one 
    NSString * titleString = ([view respondsToSelector:@selector(title)]) ? [NSString stringWithFormat:@"%@", [NSString stringWithFormat:@"\"%@\" ", [(NSButton *)view title]]] : @""; 

    // Append our own description at this level 
    [builtHierarchicalString appendFormat:@"\n%@<%@: %p> %@(%li subviews)", tabString, [view className], view, titleString, [[view subviews] count]]; 

    // Recurse for each subview ... 
    for (NSView * subview in [view subviews]) 
    [builtHierarchicalString appendString:[NSView hierarchicalDescriptionOfView:subview 
                      level:(level + 1)]]; 

    return builtHierarchicalString; 
} 

- (void)logHierarchy 
{ 
    NSLog(@"%@", [NSView hierarchicalDescriptionOfView:self 
               level:0]); 
} 

Uso

volcado esto en una categoría NSView, volcar esto en él. Incluya el encabezado de la categoría donde quiera que lo use, luego llame al [myView logHierarchy]; y mídalo.

2

Swift 4.

macOS:

extension NSView { 

    // Prints results of internal Apple API method `_subtreeDescription` to console. 
    public func dump() { 
     Swift.print(perform(Selector(("_subtreeDescription")))) 
    } 
} 

iOS:

extension UIView { 

    // Prints results of internal Apple API method `recursiveDescription` to console. 
    public func dump() { 
     Swift.print(perform(Selector(("recursiveDescription")))) 
    } 
} 

uso (en depurador): po myView.dump()

Cuestiones relacionadas