Calling print() inside a subclass of NSWindow calls NSWindow's print: method instead of console debug printing the string

Right now I'm porting a little app to Swift 2.0. As it came up I have to subclass NSPanel and handle the flagsChanged: event. Now something curious:


import Cocoa
class SearchPanel: NSPanel {
    override func flagsChanged(theEvent: NSEvent) {
        super.flagsChanged(theEvent)
        print("hello!")
    }
}


Instead of "hello!" being printed to the console the OS X printing dialog opens and wants to print the panel's contents. I guess it's because NSWindow's print(sender: AnyObject?) method is called instead of the global print<T>(val: T).


So how can I call the right print()?

Call the function from the module in which it is defined, ie use Swift.print instead of just print.


Namespacing is implicit in swift, all classes (etc) are implicitly scoped by the module (Xcode target) they are in. no class prefixes needed

- Chris Lattner


(Also, you can "Jump to Definition" by moving the caret to something you are interested in and pressing ctrl+cmd+J, or cmd clickicking on it. Doing so on the function you want (ie print() in global scope (or at least a scope where there's no other print)) will show you that it's defined within the Swift module, which is the standard library.)

Accepted Answer

I filed a,radar,on this a couple of days ago, dont have the number handy, but it is in the system.

A bug report? What for? The compiler works as designed in this case.

Just to be clear, my post above was not some sort of workaround, it's just explaining how it works, as designed. So no, this is certainly not a bug.

Calling print() inside a subclass of NSWindow calls NSWindow's print: method instead of console debug printing the string
 
 
Q