Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling `print` inside NSView opens print dialog

Tags:

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?

like image 810
Morgan Wilde Avatar asked Jun 27 '15 10:06

Morgan Wilde


People also ask

How do I close the Print dialog box?

In the printer dialog box, select the print job you want to cancel. Note: If you're using Windows 10, you might need to select the printer you're using first. Click Document > Cancel.

What does Print dialog mean?

The Print dialog box lets the user select options for a particular print job. For example, the user can specify the printer to use, the range of pages to print, and the number of copies.

What are the various options in the Print dialogue box?

You can make Media Type, Mode, Ink, Copies, and Pages settings in the Print dialog box. From this dialog box, you can also access other dialog boxes, from which you can set the print time, preview the document before printing, and save spool data as a file.

How do I open the Print dialog box?

Click File and then click Print. On the Print area of the File options, click the Print option to open the Print dialog box.


1 Answers

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.

like image 63
luk2302 Avatar answered Oct 22 '22 13:10

luk2302