ios - Cannot convert value of type () to expected argument type bool (Swift)? -


i have following code:

func savecontext () {     var error: nserror? = nil     let managedobjectcontext = self.managedobjectcontext     if managedobjectcontext != nil {         if managedobjectcontext.haschanges && !managedobjectcontext.save(){             abort()         }     } } 

i got 2 errors: call can throw, not marked try , error not handled. other 1 says cannot convert value of type () expected argument type 'bool'.

how can fix this?

the documentation nsmanagedobjectcontext save() misleading. doesn't have return value in swift. instead, can throw error.

your 2 errors because ignoring throws , treating returns bool.

your code needs like:

func savecontext () {     var error: nserror? = nil     let managedobjectcontext = self.managedobjectcontext     if managedobjectcontext != nil {         if managedobjectcontext.haschanges {             {                 try managedobjectcontext.save()             } catch {                 print("unable save: \(error)")                 abort()             }         } else {             abort()         }     } } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -