ios - call back methods with closure? -
this specific question got treehouse part one: we're writing app fetch recent blog posts treehouse blog. requires making network request using asynchronous methods execute in background. need closure. create method called fetchtreehouseblogposts, has single parameter - completion handler. closure has 3 parameters: data object containing results of request type nsdata!, http response object our request type nsurlresponse!, error object type nserror!, , return type of void.
/////////////i part , code correct following: typealias blogpostcompletion = ((nsdata!, nsurlresponse!, nserror!) -> void) func fetchtreehouseblogposts(completion: blogpostcompletion){} ///////////this part cant right. please copy paste following code inside body of method: let blogurl = nsurl(string: "http://blog.teamtreehouse.com/api/") let requesturl = nsurl(string: "get_recent_summary/?count=20", relativetourl: blogurl) let request = nsurlrequest(url: requesturl!) let config = nsurlsessionconfiguration.defaultsessionconfiguration() let session = nsurlsession(configuration: config) // add code between comments // add code above datatask.resume()
your job create data task (not download task), named datatask, completion handler returns results of data task. add data task between commented section. once that's done, within body of data task, return results fetchtreehouseblogposts method's completion handler.
make sure name type too. after datatask include nsurlsessiondatatask so...
let datatask: nsurlsessiondatatask = session.datataskwithrequest(request) {}....
& proceed statements.
Comments
Post a Comment