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

William Choi
William Choi
6,121 Points

Struggling with challenge

Cant seem to figure out how to solve the challenge.

Callbacks.swift
import Foundation

// Add your code below
        let fetchTreehouseBlogPosts = session.dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in

        })

1 Answer

Jo Albright
Jo Albright
5,199 Points

Correct direction for block. But they are requesting you create a function named "fetchTreehouseBlogPosts" that has a block parameter.

// using typealias

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

func fetchTreehouseBlogPosts(completion: BlogPostCompletion) {

}

// not using typealias

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


}
William Choi
William Choi
6,121 Points

Thanks I was just wondering also if you could help me with the second part of this challenge as well. with concerns to the dataTask, the data thats being returned is it the NSData, NSURLResponse, and the NSError?

and would I write it as a func or a let?

If you could help me with that that would be great. Cheers anyway

Jo Albright
Jo Albright
5,199 Points

It will be a let.

let dataTask = CREATE_DATATASK_METHOD_WITH_COMPLETION_BLOCK

Just replace that with the correct method that creates a dataTask with a completion block.