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

Marcelo Farjalla
Marcelo Farjalla
3,241 Points

I can't figure what code is need to pass this challenge.

import Foundation

// Add your code below typealias BlogPostCompletion = ([String: AnyObject]?) -> Void func fetchTreehouseBlogPosts(completion: BlogPostCompletion) { (let data, let response, let error) in

}

The challenge simply says to create a closure method with a completion parameter which has data, response, error as parameters of the closure. The question also mentions I should specify the types as NSData!. etc.... . In the code example we developed for the Weather app I don't see how we stated the type...it seems implied. Need some help understanding how to complete this challenge. Thanks in advance.

Callbacks.swift
import Foundation

// Add your code below
typealias BlogPostCompletion = ([String: AnyObject]?) -> Void
func fetchTreehouseBlogPosts(completion: BlogPostCompletion) {
  (let data: NSData!, let response: NSURLResponse!, let error: NSError!) in

}

1 Answer

Hi,

I got stuck just like you, then I realized I was trying to reproduce the code we made in the Stormy app, whereas this is not actually what we have been asked to do...

The challenge tells us to provide a closure to our function with NSData!, NSURLResponse! and NSError! as parameters. So basically we are passing a completion handler for later use in our session.dataTaskWithRequest method.

Thus, the correct code is :

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