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

Kyaw Kyaw Soe
Kyaw Kyaw Soe
7,528 Points

Call Back Methods With Closures

We're writing an app to fetch the most recent blog posts from the Treehouse blog. This requires making a network request using asynchronous methods that execute in the background. For that we need a closure. Create a method called fetchTreehouseBlogPosts, that has a single parameter - a completion handler. The closure has three parameters: a data object containing the results of the request as type NSData!, the HTTP response object from our request as type NSURLResponse!, an error object as type NSError!, and a return type of void. Note: If you prefer to use a typealias to make the method signature clear, name it BlogPostCompletion

That was what Challenge request me

import Foundation

// Add your code below

func fetchTreehouseBlogPosts(completion: (data:NSData!, response:NSURLResponse!, error:NSError!) -> Void) {

}

Above is my code

Please explain me more clearly or show me the right answer

1 Answer

Richard Lu
Richard Lu
20,185 Points

Hey Jo,

The syntax of your completion function type should be written like this:

(NSData!, NSURLResponse!, NSError!) -> Void // it's a function type, therefore you only need the types

Normally it would end here, but what they're expecting you to do for this specific challenge is to wrap the function type around parenthesis. The function type should eventually look something similar to this.

((NSData!, NSURLResponse!, NSError!) -> Void)

Hope I've helped. Happy Coding! :)