这很奇怪。我有一个简单的故事板占位符和GridView的类名属性。
class GridView: NSView { required init?(coder: NSCoder) { super.init(coder: coder) print("coder: \(coder)") } override func drawRect(dirtyRect: NSRect) { let rect = NSBezierPath(rect: dirtyRect) NSColor.redColor().setFill() rect.fill() } }
这只是在实现drawRect时按预期工作,但在我添加初始化程序后,每次运行应用程序时它都会开始打开打印对话框。@H_403_4@
调用print()会做一些不同的事情 – 更确切地说:就像你期望的那样不同。它调用
NSView
‘s print(sender: AnyObject?)
而不是日志打印。您可以将此视为一个错误或至少是非常意外的行为,因为Swift.print(…)通常使用得更多。
This action method opens the Print panel,and if the user chooses an option other than canceling,prints the receiver and all its subviews to the device specified in the Print panel.@H_403_4@
看看this post in the apple dev forum。@H_403_4@
事实上,这不是一个错误,因为调用当前上下文中“更接近”的打印肯定是正确的方法。调用父母的印刷品比调用一些任意的其他印刷品更合理。只有您通常使用其他打印件的事实才是令人困惑的事实,因为一般情况下您不必担心日志打印所在的范围 – 它只是起作用。如果您认为反过来并且想要使用父级的打印打印,那么必须明确声明您要使用parent print而不是Swift.print(…)会更加困惑。@H_403_4@