swift - do try in beginSheetModalForWindow -


in swift 2.0, if following:

panel.beginsheetmodalforwindow(self.view.window!) { (result) in    switch result {   // save selected   case nsfilehandlingpanelokbutton:     self.writefile(filestring, fileurl: panel.url!)     // else selected   default:     break   } } 

with function follows, compiles , works.

  func writefile(filestring: string, fileurl: nsurl) {     {       try filestring.writetourl(fileurl, atomically: true, encoding: nsutf8stringencoding)     } catch let error nserror {       fatalerror("error writing file - \(error)")     }   } 

but, if combine 2 this:

panel.beginsheetmodalforwindow(self.view.window!) { (result) in   switch result {   // save selected   case nsfilehandlingpanelokbutton:     {       try filestring.writetourl(fileurl, atomically: true, encoding: nsutf8stringencoding)     } catch let error nserror {       fatalerror("error writing file - \(error)")     }     // else selected   default:     break   } } 

it won't compile. receive compiler error:

invalid conversion throwing function of type '() throws ->' non-throwing function type '(int) -> void'

why?

add default catch block so:

do {       try filestring.writetourl(fileurl, atomically: true, encoding: nsutf8stringencoding) } catch let error nserror {       fatalerror("error writing file - \(error)") } catch {  } 

http://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -