Carbon có chức năng hữu ích được gọi là DebugPrintControlHierarchy.Làm thế nào để in một hệ thống phân cấp điều khiển trong Cocoa?
Có điều gì tương tự cho NSView hoặc NSWindow không?
Carbon có chức năng hữu ích được gọi là DebugPrintControlHierarchy.Làm thế nào để in một hệ thống phân cấp điều khiển trong Cocoa?
Có điều gì tương tự cho NSView hoặc NSWindow không?
Tôi không biết chính xác DebugPrintControlHierarchy
được in nhưng NSView có phương thức hữu ích gọi _subtreeDescription
, trả về một chuỗi mô tả toàn bộ hệ thống phân cấp bên dưới người nhận, bao gồm các lớp, khung và thông tin hữu ích khác.
Đừng lo sợ về dấu gạch dưới _
hàng đầu. Nó không phải là API công khai, nhưng nó bị xử phạt vì sử dụng công khai trong gdb. Bạn có thể thấy nó được đề cập đến in the AppKit release notes cùng với một số đầu ra mẫu.
Dưới đây là can đảm của một thể loại NSView tôi đã xây dựng một lúc quay lại:
+ (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]);
}
Cách sử dụng
Dump này vào một thể loại NSView, đổ này trong đó. Bao gồm tiêu đề danh mục ở bất cứ nơi nào bạn muốn sử dụng, sau đó gọi [myView logHierarchy];
và xem nó đi.
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"))))
}
}
sử dụng (trong debugger): po myView.dump()