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
Post a Comment