Swift print() error

I'm developing a macOS document-based app in Swift, using Xcode 11.6 and the template it provides. I often use Swift print() statements during development to see what's going on as the app runs. But when I try to put one of these in the Document.swift template file, I get errors:

Cannot convert value of type 'String' to expected argument type '[NSPrintInfo.AttributeKey : Any]'

Missing argument label 'withSettings:' in call

Missing arguments for parameters 'showPrintPanel', 'delegate', 'didPrint', 'contextInfo' in call


I don't get these errors when I put a print() statement in the AppDelegate.swift or ViewController.swift files. Can someone tell me what the problem is here?

Accepted Reply

Under the current Xcode (11.6), Xcode imports the class NSDocument as having a method print(withSettings:showPrintPanel:delegate:didPrint:contextInfo:), so, it may be shadowing the Swift.print you want to use.

Underlying Objective-C method is printDocumentWithSettings:showPrintPanel:delegate:didPrintSelector:contextInfo:, so it should be imported as printDocument(withSettings:showPrintPanel:delegate:didPrint:contextInfo:).
(As far as I remember, the Objective-C name was renamed to avoid confusion in Swift code a little time ago.)

In my opinion, this is a bug of the macOS SDK bundled with Xcode 11.6, and you should better send a feedback to Apple.

Until the issue is fixed, you can workaround using Swift.print instead of print.

Replies

Could you show the code where this occurs ? And tell what the context is (in which class does this occur).
Under the current Xcode (11.6), Xcode imports the class NSDocument as having a method print(withSettings:showPrintPanel:delegate:didPrint:contextInfo:), so, it may be shadowing the Swift.print you want to use.

Underlying Objective-C method is printDocumentWithSettings:showPrintPanel:delegate:didPrintSelector:contextInfo:, so it should be imported as printDocument(withSettings:showPrintPanel:delegate:didPrint:contextInfo:).
(As far as I remember, the Objective-C name was renamed to avoid confusion in Swift code a little time ago.)

In my opinion, this is a bug of the macOS SDK bundled with Xcode 11.6, and you should better send a feedback to Apple.

Until the issue is fixed, you can workaround using Swift.print instead of print.
Thanks to OOPer for the explanation and the solution!