[Swift 2.0] Do... Try... Catch... in savePDF

In case someone else is implementing the “savePDF” method at the end of Chapter 20 and runs into the compiler error: “[color=#BF0040]Cannot invoke ‘writeToURL’ with an argument list of type ‘(NSURL, options: NSDataWritingOptions, error: inout NSError?)’[/color]”, you apparently have to use Swift 2.0’s new exception handling mechanism embodied in the do… try… catch… syntax:

@IBAction func savePDF(sender: AnyObject!) { let savePanel = NSSavePanel() savePanel.allowedFileTypes = ["pdf"] savePanel.beginSheetModalForWindow(window!, completionHandler: { [unowned savePanel] (result) in if result == NSModalResponseOK { let data = self.dataWithPDFInsideRect(self.bounds) do { try data.writeToURL(savePanel.URL!, options: NSDataWritingOptions.DataWritingAtomic) } catch let error as NSError { let alert = NSAlert(error: error) alert.runModal() } } }) }