Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Victor Wang
Victor Wang
4,893 Points

Code Challenge

Can someone please explain to me what is required of me to do in this code challenge? I don't exactly understand the requirement here. Thank you!

" 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 "

Callbacks.swift
import Foundation

typealias BlogPostCompletion = (NSData!, NSURLResponse!, NSError!) -> Void
func fetchTreehouseBlogPosts(completion: BlogPostCompletion){

}

3 Answers

James Tench
James Tench
43,891 Points

I noticed in those examples that when the type alias is used later they are wrapped in parentheses. So the parentheses are still there, they are just typing them when using the variable. In the code challenge I made the entire type alias include the parentheses so I could just use the variable.

Victor Wang
Victor Wang
4,893 Points

Thank you for your help James! I didn't pay any attention to those parentheses until you mentioned they were there...

I tried different placements for the parentheses in the code challenge and it seems like only your solution (wrapping the entire type alias in parentheses) is valid in the treehouse system.

Again, thank you! :)

James Tench
James Tench
43,891 Points

Check the type alias. I needs to be wrapped in parentheses to indicate it is a function

Victor Wang
Victor Wang
4,893 Points

Thanks James, do you mean like this?

typealias BlogPostCompletion = ((NSData!, NSURLResponse!, NSError!) -> Void)

But this doesn't seem to be the problem here, here's an example of the parentheses not being needed.

In the example the following code seems to be legit.

typealias MyFunctionDefinition = (Integer, String) -> Void

This particular code challenge is as buggy as hell. First of all...

typealias BlogPostCompletion = (NSData!, NSURLResponse!, NSError!) -> Void

...is valid syntax according to the Swift compiler, but the code challenge insists you wrap round brackets around it, like so...

typealias BlogPostCompletion = ((NSData!, NSURLResponse!, NSError!) -> Void)

...even though Treehouse's own examples show closure typealias' without the surrounding brackets.

Also step 2 of this code challenge wouldn't accept my code, even though I was certain it was correct. So on a hunch I copied my code to the clipboard, refreshed the page, did Step 1 again, then pasted my code into Step 2 that it previously said was incorrect, and it passed it no problem.

Victor Wang
Victor Wang
4,893 Points

Haha, cheers Dominic, great to hear I'm not alone :D