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 trialWoongshik Choi
42,387 PointsWhat's wrong on this syntax in Swift?
I am not sure what's wrong on this syntax...
import Foundation
// Add your code below func fetchTreehouseBlogPosts(completionHandler: {(data: NSData!, response: NSResponse!, error: NSError!) -> Void in })
7 Answers
Grant Powell
2,356 Pointstypealias BlogPostCompletion = ((NSData!, NSURLResponse!, NSError!) -> Void) func fetchTreehouseBlogPosts (completion: BlogPostCompletion) { }
Matthew Young
5,133 PointsThe first snippet of code that Brian posted doesn't work because of the curly braces in it. Remove them and the code works fine.
func fetchTreehouseBlogPosts (completion: ((NSData!, NSURLResponse!, NSError!) -> Void)) { }
tate
13,435 PointsYou haven't really explained much but I'm guessing that the thing wrong is the fact that half of your code is commented out.
Woongshik Choi
42,387 PointsThat's my mistake.
I am doing a challenge below...
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
And my code is...
import Foundation
// Add your code below
func fetchTreehouseBlogPosts(completionHandler: {(data: NSData!, response: NSResponse!, error: NSError!) -> Void in })
So I am not sure what's wrong on my code...
Grant Powell
2,356 PointsI copied and pasted that code and still get an error.
Grant Powell
2,356 PointsIt seems there is some mistake...
1) This code does NOT work: func fetchTreehouseBlogPosts(completion: {((NSData!, NSResponse!, NSError!) -> Void)})
2) Nor does this: func fetchTreehouseBlogPosts(completion: ((NSData!, NSURLResponse!, NSError!) -> Void)) {}
3) However, this DOES work: typealias BlogPostCompletion = ((NSData!, NSURLResponse!, NSError!) -> Void) func fetchTreehouseBlogPosts (completion: BlogPostCompletion) { }
It seems to me that #2 should also work. Am I misunderstanding the syntax?
Adam Creasey
5,756 Pointsfunc fetchTreehouseBlogPosts(completion: ((NSData!, NSURLResponse!, NSError!) -> Void)){}
Not sure how you guys went, but this worked for me
Seth Rininger
586 Points(NSData!, NSURLResponse!, NSError!) -> Void..... you have to have parentheses around the whole thing like ((NSData!, NSURLResponse!, NSError!) -> Void) instead of my first one.