title: macOS Printing in Swift
body: <p>Please note this is not an iOS question.</p>
  
  <p>I have an NSView-based app (i.e. not document-based), and I’d like to bolt on a printing subsystem. I can get NSViews in my main controller to print ok. However, I want to have a special view constructed just for printing. The view should not show in the app’s window. The view contains two NSTextFields, two NSTextViews, and 5 labels.</p>
  
  <p>I cannot seem to figure out a way to do this.  I have tried various forms of these examples:</p>
  
  <ol>
  <li><p>Add an NSView to my main view window? Seems logical, but it’s awkward in a storyboard, (I can’t position the view in the storyboard).</p></li>
  <li><p>Programmatically create a custom NSView with a xib?</p></li>
  </ol>
  
  <p>For this, I’ve tried:</p>
  
  <pre><code>@IBOutlet weak var printView: NSView!
  ….
  let printOperation = NSPrintOperation(view: printView!)
  </code></pre>
  
  <p>This results in the comprehensive "fatal error: unexpectedly found nil while unwrapping an Optional value” message.</p>
  
  <p>The outlets are configured correctly (I think)</p>
  
  <ol start="3">
  <li>A seperate ViewController? If so, how can I avoid having two print buttons — one to call the print controller, and the second, to print the PrintController’s view.</li>
  </ol>
  
  <p>I’ve tried reading the Apple docs, but they are not the way I learn best. There are also no Print documents in Swift that I've found. I’ve waded through many SE questions, but have come up blank. Could you point me towards a solution please.</p>