Calling `print` inside NSView opens print dialog

后端 未结 3 412
一向
一向 2020-12-09 09:07

This is bizarre. I have a simple storyboard placeholder with GridView for the class name attribute.

class GridView: NSView {

    required init?         


        
相关标签:
3条回答
  • 2020-12-09 09:20

    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.

    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.

    0 讨论(0)
  • 2020-12-09 09:24

    This is a pretty old thread, but I must add here that you can write:

    Swift.print("something")
    

    and you would be using the "log print" function instead of view's one.

    0 讨论(0)
  • 2020-12-09 09:29

    The reason it's calling the print dialog is that Swift 2 apparently has two methods with the same signature.

    enter image description here

    0 讨论(0)
提交回复
热议问题