在NSView中调用`print`将打开“打印"对话框 [英] Calling `print` inside NSView opens print dialog
问题描述
这很奇怪.我有一个简单的情节提要占位符,其类名属性为GridView
.
This is bizarre. I have a simple storyboard placeholder with GridView
for the class name attribute.
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
即可达到预期效果,但是在添加初始化程序后,每次我运行该应用程序时它便开始打开打印对话框.
This worked as expected with just drawRect
implemented, but after I added the initialiser it started opening the print dialog every time I run the app.
为什么会发生这种情况,以及如何为自定义视图正确重新实现情节提要初始化器?
Why does this happen and how can I properly reimplement the storyboard initialiser for a custom view?
推荐答案
调用print()
会做一些不同的事情-更确切地说,是您所期望的不同.它调用 print(sender: AnyObject?)
Swift.print(...)
通常被更多地使用.
Calling print()
does something different as it should - more precisely: something different as you would expect. It calls NSView
's print(sender: AnyObject?)
instead of the logging print. You could consider this as a bug or at least as quite unexpected behavior since the Swift.print(...)
is generally much more used.
此操作方法将打开打印"面板,如果用户选择了取消"以外的选项,则会将接收器及其所有子视图打印到打印"面板中指定的设备上.
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.
在苹果开发者论坛上查看这篇文章.
Take a look at this post in the apple dev forum.
实际上这不是错误,因为调用print
在当前上下文中是"closer"无疑是正确的方法.调用父级的print
比调用任意其他print
更为合理.在这里,只有通常使用 other 打印的事实才是令人困惑的地方,因为通常您不必担心日志print
所处的范围-它可以正常工作.如果您想换一种方式,并想使用父母的印刷品print
,则必须明确声明您要使用的 lot 更加令人困惑父母print
而不是Swift.print(...)
.
In fact it is not a bug since calling the print
which is "closer" in the current context is certainly the correct way. Calling the parent's print
is a lot more reasonable than calling some arbitrary other print
. Only the fact that you normally use the other print is the confusing point here since in general you do not worry in what scope the logging print
is located - it just works. If you think the other way around and would want to use the printing print
of your parent it would be a lot more confusing having to explicitly state that you want to use the parents print
and not the Swift.print(...)
.
唯一的解决方案" 是为这两个函数使用不同的名称,这可能不会发生.
The only "solution" would be to use different names for the two functions which is probably not going to happen.
这篇关于在NSView中调用`print`将打开“打印"对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!