ios - Swift JSON Crash with Nil -


i have problem.. have app connects database json, problem cannot find element in response of database. code :

 func uploadtodb(){     var selectvehicle = save.stringforkey("selectvehiclechoosed")      if selectvehicle == nil  {          var alertview:uialertview = uialertview()         alertview.title = "submit failed!"         alertview.message = "please select vehicle"         alertview.delegate = self         alertview.addbuttonwithtitle("ok")         alertview.show()       }else {          var post:nsstring = "vechiclenumber=\(selectvehicle)"          nslog("postdata: %@",post);          var url:nsurl = nsurl(string: "http://saimobileapp.com/services/sai_service_history.php?")!          var postdata:nsdata = post.datausingencoding(nsasciistringencoding)!         var postlength:nsstring = string( postdata.length )         var 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? = nsurlconnection.sendsynchronousrequest(request, returningresponse:&response, error:&reponseerror)          if ( urldata != nil ) {             let res = response as! nshttpurlresponse!;              nslog("response code: %ld", res.statuscode);              if (res.statuscode >= 200 && res.statuscode < 300)             {                 var responsedata:nsstring  = nsstring(data:urldata!, encoding:nsutf8stringencoding)!                  nslog("response ==> %@", responsedata);                 println(responsedata)                 var error: nserror?                  let jsondata:nsdictionary = nsjsonserialization.jsonobjectwithdata(urldata!, options:nsjsonreadingoptions.mutablecontainers , error: &error) as! nsdictionary                   var servicedate = ((jsondata nsdictionary)["servicehistory"] as! nsdictionary) ["servicedate"] as! string                 var servicetype = ((jsondata nsdictionary)["servicehistory"] as! nsdictionary) ["servicetype"] as! string                 var kms = ((jsondata nsdictionary)["servicehistory"] as! nsdictionary) ["mileage"] as! string                 var servicelocation = ((jsondata nsdictionary)["servicehistory"] as! nsdictionary) ["servicelocation"] as! string                   var serviced:void = save.setobject(servicedate, forkey: "servicedatechoosed")                 var servicet:void = save.setobject(servicetype, forkey: "servicetypechoosed")                 var kmsc:void = save.setobject(kms, forkey: "kmschoosed")                 var servicel:void = save.setobject(servicelocation, forkey: "servicelocationchoosed")                               save.synchronize()                   tableview.reloaddata()              }         }      } } 

and response

{"servicehistory":[{"id":"2","vehiclenumber":"mh03aw0001","mobilenumber":"9503322593","customername":"samsun","servicedate":"2012-06-02","servicetype":"paid service","mileage":"65","servicestate":"maharashtra","servicelocation":"lower parel","puc":""}]} 

the app crash in line var servicedate = ((jsondata nsdictionary)["servicehistory"] as! nsdictionary) ["servicedate"] as! string nil because think can't find element.

thanks in advance help.

// servicehistory array var servicehistoryarray = jsondata["servicehistory"] as! nsarray  // fetch first item... var servicehistoryitem = servicehistoryarray[0] as! nsdictionary var servicedate = servicehistoryitem["servicedate"] var servicetype = servicehistoryitem["servicetype"] var kms = servicehistoryitem["mileage"] var servicelocation = servicehistoryitem["servicelocation"] 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -