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

Enrique Munguía
Enrique Munguía
14,311 Points

Error in code challenge

In step 2/2 I'm getting an error, I tried to put the code in xcode and it works, if anyone can spot my error would be nice. The code I added is between comments. Thank you.

Callbacks.swift
import Foundation

// Add your code below
typealias BlogPostCompletion = ((NSData!, NSURLResponse!, NSError!) -> Void)

func fetchTreehouseBlogPosts(handler: BlogPostCompletion) {
  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,
        completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in

        handler(data, response, error)

    })
  // Add code above

  dataTask.resume()
}

1 Answer

I think your example is correct, but I think this code challenge is a bit buggy, so try the following between the comments...

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

    completion(data, response, error)
}

I couldn't even get this to work initially until I copied the entire code file, refreshed the page and copied it back into Step 2 with no changes, and it worked.

Enrique Munguía
Enrique Munguía
14,311 Points

Yeah I tried what you suggested and it worked, thank you.

Corey Dutson
Corey Dutson
Courses Plus Student 3,193 Points

Ugh. Thanks for this. I was bashing my head against a wall trying to figure out what the hell was going on. FYI the code I was using (that didn't work) was:

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

Basically the same, but with slight indentation differences. I'd chalk this one up to the code checker spazzing out.