How to get data from a Swift NSURLSession? -
How to get data from a Swift NSURLSession? -
for example, have next code:
let task = nsurlsession.sharedsession().datataskwithurl(url, completionhandler: {data, response, error -> void in var dann = nsstring(data: data, encoding: nsutf8stringencoding)! self.str = dann }) task.resume()
i want transfer info variable in class (the str variable in class). string "self.str = dann" not convey anything. how can this?
i'm not sure nsstring type want. json may format of info returned, depending on url's functionality. tried code provided , got same issues, if treat json (i used httpbin.org dummy url source) , worked.
allow task = nsurlsession.sharedsession().datataskwithurl(nsurl(string: "http://httpbin.org/get")!, completionhandler: { (data, response, error) -> void in do{ str = seek nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.allowfragments) as! [string:anyobject] print(str) } grab { print("json error: \(error)") } }) task.resume()
(thanks suggested edit @sgthad. edit not particularly relevant question, still wanted update code current.)
swift nsurlsession
Comments
Post a Comment