ios - how to throw errors in a closure in swift? -
please @ following code:
override func tableview(tableview: uitableview, editactionsforrowatindexpath indexpath: nsindexpath) -> [uitableviewrowaction]? { let deleteaction = uitableviewrowaction(style: uitableviewrowactionstyle.default, title: "delete", handler: { (action : uitableviewrowaction, indexpath : nsindexpath) -> void in if let managedobjectcontext = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext{ let restauranttodelete = self.fetchresultcontroller.objectatindexpath(indexpath) as! restaurant managedobjectcontext.deleteobject(restauranttodelete) // saving managedobjectcontext instance, , catch errors if fails { try managedobjectcontext.save() } catch let error nserror { print("error: \(error.localizeddescription)") } } }) return deleteaction }
the error message xcode : invalid conversion throwing function of type '(uitableviewrowaction, nsindexpath) throws -> void' non-throwing function type '(uitableviewrowaction, nsindexpath) -> void'
i know problem managedobjectcontext.save() throw errors , not allowed in completion handler. found blog articles modified closure parameters in order make error handling in closure workable. while here definition of function given apple, how can fix issue? lot! :d
the compiler adding throws
signature of block because catch
clause not exhaustive: pattern match let error nserror
can fail... see documentation
the signature of closure argument (uitableviewrowaction, nsindexpath) -> void
, compiler inferring type of closure providing (uitableviewrowaction, nsindexpath) throws -> void
by adding catch
clause (with no pattern) after 1 have compiler see catching exception locally , no longer infer signature of closure providing includes throws
:
do { try managedobjectcontext.save() } catch let error nserror { print("error: \(error.localizeddescription)") } catch {}
Comments
Post a Comment