Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift Managing Complexity Callback Methods With Closures

This Weather app question literally makes no sense

I have no idea what they want for this question. It says that in the body of a the dataTask I need to return the information, but first off dataTask does not have a body because it is just an object thats returned from a method call, but I guess they are talking about the closure that is a parameter of the method dataTaskWithRequest. However this still makes no sense because I cannot call any function that is outside of the closure. If you just call someFunction() from within the closure and then define a simple func someFunction() {} outside of the closure you get an "Use of unresolved identifier 'someFunction'" error. So if I cannot even call the fetchTreehouseBlogPosts function from within the closure, then the instructions for this question are wrong. Also, even if you could call the fetchTreehouseBlogPosts function from within the closure, then what is the point? Am I literally just supposed to write fetchTreehouseBlogPosts({(data, response, error) in })with no body? I mean whats the point? I have no idea what purpose the extra function serves. To me, if you need to do something with the data, response or error parameters, then why wouldn't you just do it in the closure, instead of including some weird extra function. It makes no sense to me.

Callbacks.swift
import Foundation

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 your code between the comments
let dataTask = session.dataTaskWithRequest(request) {
    (let data, let response, let error) in

    fetchTreehouseBlogPosts({(data, response, error) in })

})
// Add code above

dataTask.resume()

// Add your code below

func fetchTreehouseBlogPosts(completionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)) {

}

For starters, it's asking you to copy the code and paste it into the body of the method, like so:

func fetchTreehouseBlogPosts(completion: ((NSData!, NSURLResponse!, NSError!) -> Void)) {
  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 your code between the comments

  // Add code above

  dataTask.resume()
}

Then it's asking you to return the results to the completion handler of the method, which looks like this:

 let dataTask = session.dataTaskWithRequest(request) { (let data, let response, let error) in 
    completion(data, response, error)
}

So what this line is doing is calling the completion parameter which belongs to the method signature, and you pass it in your data from the dataTaskWithRequest closure.

The finished looks like the following:

import Foundation

// Add your code below
func fetchTreehouseBlogPosts(completion: ((NSData!, NSURLResponse!, NSError!) -> Void)) {
  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 your code between the comments
  let dataTask = session.dataTaskWithRequest(request) { (let data, let response, let error) in 
    completion(data, response, error)
  }
  // Add code above

  dataTask.resume()
}