如何在Cocoa中打印控件层次结构?
Carbon有一个名为DebugPrintControlHierarchy的有用函数如何在Cocoa中打印控件层次结构?,cocoa,macos,Cocoa,Macos,Carbon有一个名为DebugPrintControlHierarchy的有用函数 NSView或NSWindow是否有类似的功能?我不知道究竟打印了什么DebugPrintControlHierarchy,但NSView有一个有用的方法调用\u subtreeDescription,它返回一个字符串,描述接收器下的整个层次结构,包括类、帧和其他有用的信息 不要害怕前面的下划线。它不是公共API,但在gdb中被批准用于公共用途。您可以看到它与一些示例输出一起被提到。以下是我不久前构建的NSVi
NSView或NSWindow是否有类似的功能?我不知道究竟打印了什么
DebugPrintControlHierarchy
,但NSView有一个有用的方法调用\u subtreeDescription
,它返回一个字符串,描述接收器下的整个层次结构,包括类、帧和其他有用的信息
不要害怕前面的下划线。它不是公共API,但在gdb中被批准用于公共用途。您可以看到它与一些示例输出一起被提到。以下是我不久前构建的NSView类别的精髓:
+ (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]);
}
+(NSString*)层次结构描述视图:(NSView*)视图
级别:(整数)级别
{
//准备好此级别的描述字符串
NSMutableString*builtHierarchicalString=[NSMutableString];
//为当前级别的缩进生成选项卡字符串
NSMutableString*tabString=[NSMutableString];
对于(i=0;i4
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
iOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
用法(在调试器中):po myView.dump()