23

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()
    }
}

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.

Print dialog

Why does this happen and how can I properly reimplement the storyboard initialiser for a custom view?

4
  • 2
    have you tried removing the print statement? might be a very strange bug...
    – luk2302
    Jun 27, 2015 at 10:34
  • Oh wow! You're right, print is the reason it's doing that... This truly boggles the mind! Jun 27, 2015 at 10:36
  • well, it should just not do that, print is supposed to do something quite different... :D
    – luk2302
    Jun 27, 2015 at 10:37
  • 1
    Related: Selecting global or object function.
    – Martin R
    Jun 27, 2015 at 10:56

3 Answers 3

21

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.

9
  • Funny thing is, in the thread over at page you linked, people consider this as certainly not a bug. :) Jun 27, 2015 at 10:44
  • @MorganWilde yeah, that´s why expanded my answer - i might want to remove the "bug" word, because it is just strange but somewhat reasonable to behave that way.
    – luk2302
    Jun 27, 2015 at 10:45
  • 2
    This is a bug. The debug print is much more heavily used than print the Cocoa printing method, so the framework should do something about that. It's just bad design. Jun 27, 2015 at 10:47
  • @MorganWilde kind of, but just from a compiler perspective it is not really a bug because calling the print of the object you are currently in is actually the more correct way instead of calling some random static print that is defined somewhere else.
    – luk2302
    Jun 27, 2015 at 10:50
  • sure, it's not the compiler that's at fault here, it's the print method on these Cocoa classes that needs changing. IMHO Jun 27, 2015 at 10:52
2

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

enter image description here

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.