ios - JSON Parsing error with Swift -
i'm new in ios programming , i'm trying new language swift. i've problem / don't know how in parsing json swift ios use. used json in android, know json , link right, when try code below (seen in tutorial) app seems crash , highlighted line:
var jsonresult = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &err) nsdictionary
the console gives me error:
the operation couldn’t completed. (nsurlerrordomain error -1005.) fatal error: unexpectedly found nil while unwrapping optional value
here entire code of button's action:
let urlasstring = "http://xxxxxxxxxx.altervista.org/app/_dd_/downloadutenti.php?email="+campoemail.text+"&password="+campopassword.text let url = nsurl(string: urlasstring)! let urlsession = nsurlsession.sharedsession() println(url) println(urlsession) let jsonquery = urlsession.datataskwithurl(url, completionhandler: { data, response, error -> void in if (error != nil) { println(error.localizeddescription) } var err: nserror? if(data != nil){ var jsonresult = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &err) nsdictionary if (err != nil) { println("json error \(err!.localizeddescription)") } var jsonemail = "" if var jsonemail: string! = jsonresult["email"] as? nsstring{ } else{ println("problem 1") } var jsonpassword = "" if var jsonpassword: string! = jsonresult["pass"] as? nsstring{ } else{ println("problem 2") } dispatch_async(dispatch_get_main_queue(), { self.scritta2.text = "email: " + jsonemail + " - password: " + jsonpassword }) } }) jsonquery.resume()
n.b. use xcode6 simulator , know sure "variables" campoemail.text , campopassword.text taken in way.
json should give back:
[{"id":"1","email":"lincoln@gmail.com","password":"pass","permessi":"1","stato":"italia","citta":"palermo","via":"via lincoln, 29","cap":"90100","telefono":"091xxxxxx"}]
edit @neo help: edited action of button this, code goes data's null check..
let cururl: nsurl = nsurl(string: "http://fantacharleston.altervista.org/app/_dd_/downloadutenti.php?email=lincoln@gmail.com&password=pass")! let currequest: nsurlrequest = nsurlrequest(url: cururl, cachepolicy: nsurlrequestcachepolicy.reloadignoringlocalandremotecachedata, timeoutinterval: 60) nsurlconnection.sendasynchronousrequest(currequest, queue: nsoperationqueue.mainqueue()) { (response: nsurlresponse!, data: nsdata?, error: nserror!) -> void in if (data != nil) { if let jsonarray: nsarray = nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsarray? { let jsonobject: nsdictionary? = jsonarray.objectatindex(0) as? nsdictionary if (jsonobject != nil) { nslog("object: %@", jsonobject!) let email: nsstring? = jsonobject?.objectforkey("email") as? nsstring let password: nsstring? = jsonobject?.objectforkey("password") as? nsstring if (email != nil && password != nil) { dispatch_async(dispatch_get_main_queue(), { () -> void in self.scritta2.text = "email: " + email! + " - password: " + password! }) } } } } else{ println("null") } }
yes think know problem is... trying put jsonarray nsdictionary... cant go... trying put jsonobject in java.
just exchange nsdictionary nsarray , sure properties array[0]["key"], not array["key"], because working array, not dictionary... (its arraylist, , hashmap in java)
try please...
if let jsonresult: nsarray = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &err) nsarray? { let jsonemail: nsstring? = jsonresult.objectatindex(0).objectforkey("email") }
edit code need
here is... wrote bit write in java android... think it... problem commented maybe there no "pass" tag in json... "password"...
let cururl: nsurl = nsurl(string: "http://fantacharleston.altervista.org/app/_dd_/downloadutenti.php?email=lincoln@gmail.com&password=pass")! let currequest: nsurlrequest = nsurlrequest(url: cururl, cachepolicy: nsurlrequestcachepolicy.reloadignoringlocalandremotecachedata, timeoutinterval: 60) nsurlconnection.sendasynchronousrequest(currequest, queue: nsoperationqueue.mainqueue()) { (response: nsurlresponse!, data: nsdata?, error: nserror!) -> void in if (data != nil) { if let jsonarray: nsarray = nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsarray? { let jsonobject: nsdictionary? = jsonarray.objectatindex(0) as? nsdictionary if (jsonobject != nil) { nslog("object: %@", jsonobject!) let email: nsstring? = jsonobject?.objectforkey("email") as? nsstring let password: nsstring? = jsonobject?.objectforkey("password") as? nsstring if (email != nil && password != nil) { dispatch_async(dispatch_get_main_queue(), { () -> void in self.scritta2.text = "email: " + email + " - password: " + password }) } } } } }
Comments
Post a Comment