ios - try - throws in IBAction - xCode 7 -


i have problem new "throws" - "try" parameters.

i error add these words , added "throws" @ end of ibaction function.

@ibaction func loginbuttonpressed(sender: anyobject)throws {//code here} 

inside i'm dong irrelevant stuff , this:

let jsondata:nsdictionary = try nsjsonserialization.jsonobjectwithdata(urldata!, options:nsjsonreadingoptions.mutablecontainers ) as! nsdictionary 

after there no errors when run app , click on button, app crashes code

2015-06-15 10:43:11.374 bottled[7283:1175818] -[bottled.loginviewcontroller loginbuttonpressed:]: unrecognized selector sent instance 0x7fee28da7d20 2015-06-15 10:43:11.381 bottled[7283:1175818] * terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[bottled.loginviewcontroller loginbuttonpressed:]: unrecognized selector sent instance 0x7fee28da7d20' * first throw call stack: ( 0 corefoundation 0x000000010d67c885 exceptionpreprocess + 165 1 libobjc.a.dylib 0x000000010cd0edf1 objc_exception_throw + 48 2 corefoundation 0x000000010d684b5d -[nsobject(nsobject) doesnotrecognizeselector:] + 205 3 corefoundation 0x000000010d5d1e3a ___forwarding_ + 970 4 corefoundation 0x000000010d5d19e8 _cf_forwarding_prep_0 + 120 5 uikit 0x000000010dc46257 -[uiapplication sendaction:to:from:forevent:] + 125 6 uikit 0x000000010dc461b2 -[uiapplication sendaction:totarget:fromsender:forevent:] + 79 7 uikit 0x000000010dda1422 -[uicontrol sendaction:to:forevent:] + 67 8 uikit 0x000000010dda16c6 -[uicontrol _sendactionsforevents:withevent:] + 272 9 uikit 0x000000010dda08a9 -[uicontrol touchesended:withevent:] + 599 10 uikit 0x000000010dcacbe8 -[uiwindow _sendtouchesforevent:] + 835 11 uikit 0x000000010dcad7d6 -[uiwindow sendevent:] + 865 12 uikit 0x000000010dc62705 -[uiapplication sendevent:] + 263 13 uikit 0x000000010dc3f5df _uiapplicationhandleeventqueue + 6031 14 corefoundation 0x000000010d5a70f1 cfrunloop_is_calling_out_to_a_source0_perform_function + 17 15 corefoundation 0x000000010d59ceac __cfrunloopdosources0 + 556 16 corefoundation 0x000000010d59c363 __cfrunlooprun + 867 17 corefoundation 0x000000010d59bd78 cfrunlooprunspecific + 488 18 graphicsservices 0x0000000111fdebca graphicsservices + 52170 19 uikit 0x000000010dc4479b uiapplicationmain + 171 20 bottled 0x000000010c7cb9ad main + 109 21 libdyld.dylib 0x000000011029ca05 libdyld.dylib + 10757 22 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating uncaught exception of type nsexception (lldb)

when breakpoints , delete "throws", app not crash. don't know now.. hope can me!

the full code is:

@ibaction func loginbuttonpressed(sender: anyobject)throws {     var name:nsstring = textfieldname.text!     let pass:nsstring = textfieldpw.text!     if ( name.isequaltostring("") || pass.isequaltostring("") ) {         let loginfailed = uialertview(title: "failed signup",             message: "please fill in textfields",             delegate: nil,             cancelbuttontitle: "ok")         loginfailed.show()     } else {              let post:nsstring = "username=\(name)&password=\(pass)"              nslog("postdata: %@",post);              let url:nsurl = nsurl(string: "my url")!              let postdata:nsdata = post.datausingencoding(nsasciistringencoding)!              let postlength:nsstring = string( postdata.length )              let request:nsmutableurlrequest = nsmutableurlrequest(url: url)             request.httpmethod = "post"             request.httpbody = postdata             request.setvalue(postlength string, forhttpheaderfield: "content-length")             request.setvalue("application/x-www-form-urlencoded", forhttpheaderfield: "content-type")             request.setvalue("application/json", forhttpheaderfield: "accept")               var reponseerror: nserror?             var response: nsurlresponse?              var urldata: nsdata?             {                 urldata = try nsurlconnection.sendsynchronousrequest(request, returningresponse:&response)             } catch let error nserror {                 reponseerror = error                 urldata = nil             }              if ( urldata != nil ) {                 let res = response as! nshttpurlresponse!;                  nslog("response code: %ld", res.statuscode);                  if (res.statuscode >= 200 && res.statuscode < 300)                 {                     let responsedata:nsstring  = nsstring(data:urldata!, encoding:nsutf8stringencoding)!                      nslog("response ==> %@", responsedata);                        let jsondata:nsdictionary = try nsjsonserialization.jsonobjectwithdata(urldata!, options:nsjsonreadingoptions.mutablecontainers ) as! nsdictionary                       let success:nsinteger = jsondata.valueforkey("success") as! nsinteger                      //[jsondata[@"success"] integervalue];                      nslog("success: %ld", success);                      if(success == 1)                     { 

in obj-c throws syntactic sugar obj-c error: (nserror **)error parameter.

when add throws method, secretly adding parameter. method won't loginbuttonpressed: anymore, loginbuttonpressed:error: (at least obj-c). however, when event being handled, system still method loginbuttonpressed: because that's how event has been connected in xib. method not there anymore, app crash unrecognized selector sent instance.

solution: don't use throws in action handlers, violating contract of method because system doesn't expect thrown there. catch errors inside action handler.

do {     urldata = try nsurlconnection.sendsynchronousrequest(request, returningresponse:&response) } catch let error nserror {     reponseerror = error     urldata = nil } catch {    // catch error-handling     urldata = nil } 

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 -