如何在Cocoa中打印控件层次结构?

时间:2010-11-12 16:36:02

标签: cocoa macos

Carbon有一个名为DebugPrintControlHierarchy的有用函数。

NSView或NSWindow有类似内容吗?

3 个答案:

答案 0 :(得分:44)

我不知道究竟是什么DebugPrintControlHierarchy打印,但NSView有一个有用的方法调用_subtreeDescription,它返回一个字符串,描述接收器下面的整个层次结构,包括类,框架和其他有用的信息

不要害怕领先的_下划线。它不是公共API,但它在gdb中被公开使用。您可以看到它提到in the AppKit release notes以及一些示例输出。

答案 1 :(得分:5)

这是我后来构建的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]);
}

用法

将其转储到NSView类别中,将其转储到其中。在您想要使用的任何地方添加类别标题,然后拨打[myView logHierarchy];并观看它。

答案 2 :(得分:2)

斯威夫特4。

<强> MACOS

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

x_axis = ["A", "B","C","D","E","F"]
y_axis = [78.5, 79.6, 81.6, 75.4, 78.3, 79.6]

plt.ylabel('Accuracy')
plt.title('Accuracy of Classifier')

g=sns.barplot(x_axis, y_axis, color="red")
ax=g
#annotate axis = seaborn axis
for p in ax.patches:
             ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
                 ha='center', va='center', fontsize=11, color='gray', xytext=(0, 20),
                 textcoords='offset points')
_ = g.set_ylim(0,120) #To make space for the annotations

<强>的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")))) } }