completionhandler - How to get data to return from NSURLSessionDataTask in Swift -
completionhandler - How to get data to return from NSURLSessionDataTask in Swift -
i have same question asked , answered here: how info homecoming nsurlsessiondatatask
the difference: how can in swift? not know objective c @ trying interpret reply proving bit futile me..
so given code below have firstviewcontroller phone call homemodel class go , info using nsurlsession call. 1 time phone call finish want homecoming info firstviewcontroller may go , set in view.
firstviewcontroller class looks like:
import uikit class firstviewcontroller: uiviewcontroller { allow homemodel = homemodel() override func viewdidload() { super.viewdidload() // go load home page content sethomecontent() } override func didreceivememorywarning() { super.didreceivememorywarning() } /*call go home page content*/ func sethomecontent() { homemodel.gethomedata(); //todo find way result here... , set textview } }
homemodel class looks like:
import foundation class homemodel { private allow sitecontent:string = "http://www.imoc.co.nz/mobileapp/homecontent" func gethomedata() { var url : nsurl! = nsurl(string:sitecontent) var request: nsurlrequest = nsurlrequest(url:url) allow config = nsurlsessionconfiguration.defaultsessionconfiguration() allow session = nsurlsession(configuration: config) allow task : nsurlsessiondatatask = session.datataskwithrequest(request, completionhandler: {(data, response, error) in var error: autoreleasingunsafemutablepointer<nserror?> = nil allow jsonresult: nsdictionary! = nsjsonserialization.jsonobjectwithdata(data, options: nil, error: error) as? nsdictionary! // @ stage want jsonresult returned firstviewcontroller }); // whatever need task task.resume() }
i guess want either way pass in completion handler original or similar c# 5 using async tasks await result..
any help appreciated..
with help , suggestion taken abizern managed how write block or closure if want.
so worked me in end:
the gethomedata function changed follows:
private allow sitecontent:string = "http://www.imoc.co.nz/mobileapp/homecontent" // changed signiture original question func gethomedata(completionhandler: ((nsdictionary!) -> void)?) { var url : nsurl! = nsurl(string:sitecontent) var request: nsurlrequest = nsurlrequest(url:url) allow config = nsurlsessionconfiguration.defaultsessionconfiguration() allow session = nsurlsession(configuration: config) allow task : nsurlsessiondatatask = session.datataskwithrequest(request, completionhandler: {(data, response, error) in var error: autoreleasingunsafemutablepointer<nserror?> = nil allow jsonresult: nsdictionary! = nsjsonserialization.jsonobjectwithdata(data, options: nil, error: error) as? nsdictionary! // on finish phone call completionhandler... completionhandler?(jsonresult?); }); task.resume() }
then phone call function this:
/*call go home page content*/ func sethomecontent() { homemodel.gethomedata(self.resulthandler) } func resulthandler(jsonresult:nsdictionary!) { allow sitecontent = jsonresult.objectforkey("sitecontent") nsdictionary allow paraone = sitecontent.objectforkey("homepageparagraphone") string }
swift completionhandler nsurlsessiontask
Comments
Post a Comment