ios - get the value form mySQL to swift by PHP -
i have php file return json_encode value , when go http address give me value cant value form server side apps have try many time not
func loaddata() { let url = nsurl(string: "http://example.com/getexpo.php") let request = nsmutableurlrequest(url: url!) // modify request necessary, if necessary nsurlsession.sharedsession().datataskwithrequest(request, completionhandler: { (data:nsdata?, response:nsurlresponse?, error:nserror?) -> void in if error != nil { // display alert message print(error) return } { let json = try nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.mutablecontainers) as? nsdictionary if (json != nil) { //let userid = parsejson["userid"] as? string // display alert message let usermessage = json!["id"] as? string print(usermessage) } else { // display alert message let usermessage = "could not fetch value" print(usermessage) } } catch { print(error) } }).resume() }
any 1 can , thank !!
your json response array of dictionaries:
[{"id":"115","exponame":"aziz","expodetails":"aziz","expophone":"aziz","expolocation":"aziz"}]
but you're trying cast dictionary:
let json = try nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.mutablecontainers) as? nsdictionary
the solution of course cast array:
let json = try nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.mutablecontainers) as? nsarray
better use swift types if can:
let json = try nsjsonserialization.jsonobjectwithdata(data!, options: []) as? [[string:anyobject]]
then example can use loop:
if let json = try nsjsonserialization.jsonobjectwithdata(data!, options: []) as? [[string:anyobject]] { item in json { let usermessage = item["id"] as? string } }
Comments
Post a Comment