php - Retrieving data from MySQL and Displaying it -
i've encountered problem when working on private project. improved environment buying virtual private server (vps). installed apache, mysql , php able save data in database followed parsing php , final stage displaying on ios app i'm creating.
i looked through quite few tutorials on internet on how retrieve data php file , show in tableview , first problem encountered. i'm not going use tableview , reason have been struggling getting data dictionary/array without issues.
problem: i've followed tutorial , made work tableview when trying customize output don't manage right.
i information saved dictionary when try use dictionary in kind of way breakpoint , simulation stops.
code:
class - locationmodel.swift
import foundation class locationmodel: nsobject { var id: string? var name: string? var address: string? var city: string? var country: string? var typ: string? var lastresult: string? override init() { } init(id: string, name: string, address: string, city: string, country: string, typ: string, lastresult: string) { self.id = id self.name = name self.address = address self.city = city self.country = country self.typ = typ self.lastresult = lastresult } override var description: string { return "name: \(name), address: \(address)" } }
class - homemodel.swift
import foundation protocol homemodelprotocal: class { func itemsdownloaded(items: nsarray) } class homemodel: nsobject, nsurlsessiondatadelegate { weak var delegate: homemodelprotocal! var data : nsmutabledata = nsmutabledata() let urlpath: string = "http://server.truesight.se/risswiftphp/phptest.php" func downloaditems() { let url: nsurl = nsurl(string: urlpath)! var session: nsurlsession! let configuration = nsurlsessionconfiguration.defaultsessionconfiguration() session = nsurlsession(configuration: configuration, delegate: self, delegatequeue: nil) let task = session.datataskwithurl(url) task.resume() } func urlsession(session: nsurlsession, datatask: nsurlsessiondatatask, didreceivedata data: nsdata) { self.data.appenddata(data) } func urlsession(session: nsurlsession, task: nsurlsessiontask, didcompletewitherror error: nserror?){ if error != nil { print("failed download data") } else { print("data downloaded") self.parsejson() } } func parsejson() { var jsonresult: nsmutablearray = nsmutablearray() { jsonresult = try nsjsonserialization.jsonobjectwithdata(self.data, options:nsjsonreadingoptions.allowfragments) as! nsmutablearray } catch let error nserror { print(error) } var jsonelement: nsdictionary = nsdictionary() let locations: nsmutablearray = nsmutablearray() item in jsonresult { jsonelement = item as! nsdictionary let location = locationmodel() if let id = jsonelement["id"] as? string, let name = jsonelement["name"] as? string, let address = jsonelement["address"] as? string, let city = jsonelement["city"] as? string, let country = jsonelement["country"] as? string, let typ = jsonelement["typ"] as? string, let lastresult = jsonelement["lastresult"] as? string { print(id + name + address + country + city + typ + lastresult) location.id = id location.name = name location.address = address location.city = city location.country = country location.typ = typ location.lastresult = lastresult } if let name = jsonelement["name"] as? string, let address = jsonelement["address"] as? string { location.name = name location.address = address } locations.addobject(location) } dispatch_async(dispatch_get_main_queue(), {() -> void in self.delegate.itemsdownloaded(locations) }) } }
it un method/function parsejson in class homemodel breakpoint appears. it's let location = locationmodel()
breaks code. i've tried search through debug more information , been using po $arg0 on threads try find more error message errored out in execute, couldn't preparetoexecutejitexpression
i jsonresult
nsmutablearray filled information , jsonelement
after breaks.
i appreciate in matter i'm out of hair on head because of problem. if find solution bad or want more information please ask.
i feel i've solved i'm not sure if it's ultimate solution. moved following section parserjson urlsession.
from parsejson
var jsonresult: nsmutablearray = nsmutablearray() { jsonresult = try nsjsonserialization.jsonobjectwithdata(self.data, options:nsjsonreadingoptions.allowfragments) as! nsmutablearray } catch let error nserror { print(error) }
to function urlsession
func urlsession(session: nsurlsession, task: nsurlsessiontask, didcompletewitherror error: nserror?){ if error != nil { print("failed download data") } else { print("data downloaded") var jsonresult: nsmutablearray = nsmutablearray() { jsonresult = try nsjsonserialization.jsonobjectwithdata(self.data, options:nsjsonreadingoptions.allowfragments) as! nsmutablearray } catch let error nserror { print(error) } // array jsonresult, // sent viewcontroller control output. print(jsonresult) var vc = viewcontroller() vc.somemethod(jsonresult) } }
Comments
Post a Comment