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

Keli'i Martin
Keli'i Martin
8,227 Points

Unsure why I am not getting the correct answer

I am unsure why the answer I am submitting is coming back as incorrect. The message I get simply tells me to make sure the method is named fetchTreehouseBlogPosts and that I am using the correct syntax. The syntax I am using looks the same as the syntax that was used in the Stormy app's NetworkOperation class. The only thing I haven't done is put anything inside the fetchTreehouseBlogPosts method, because the instructions don't tell me to do so. I assumed that since this is challenge task 1 of 2 that the implementation of the function would be part 2. Is that a correct assumption for me to make?

Callbacks.swift
import Foundation

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

func fetchTreehouseBlogPosts(BlogPostCompletion)
{
}

1 Answer

Michael Reining
Michael Reining
10,101 Points

You are close. There is nothing wrong with your code but they are looking for something slightly different below.

Step 1:

// Step 1
func fetchTreehouseBlogPosts(completion: ((NSData!, NSURLResponse!, NSError!) -> Void)){}

Step 2:

// Step 2 insert this
let dataTask: NSURLSessionDataTask = session.dataTaskWithRequest(request)
    {(data, response, error) in return completion(data, response, error)}
// Step 2 alternate answer
let dataTask: NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in return completion(data, response, error)})

Complete answer looks like this.

// Step 2 fully completed looks like this

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: NSURLSessionDataTask = session.dataTaskWithRequest(request)
    {(data, response, error) in return completion(data, response, error)}
// Add code above

dataTask.resume()

I hope that helps,

Mike

PS: Thanks to the awesome resources on Team Treehouse, I launched my first app.

Now you can practice writing Swift code directly on your iPhone :-)

Code! Learn how to program with Swift

Keli'i Martin
Keli'i Martin
8,227 Points

Thanks, that works. It's just confusing because they add the little note talking about using a typealias, which is what I tried to do, but apparently you can't use the typealias if you want to pass that task.

Thanks for the help!

peterwu2
peterwu2
3,083 Points

Thank you, this works! I'm so confused with the closure and completion handler and couldn't have passed the challenge without your help.