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 Swift Closures Closures in iOS Using a Closure

Wouter Bouma
Wouter Bouma
3,229 Points

downloadTask does not compile

I entered (like in the video) let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) let url = NSURL(string: "http://blog.teamtreehouse.com/api/get_recent_summary/?count=20") let request = NSURLRequest(URL: url!)

let downloadTask = session.downloadTaskWithRequest(request, completionHandler: {(url: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

// Execute the body of the closure

})

I get the error: (NSURL!, NSURLResponse!, NSError!) ->Void is not convertible to (NSURL?, NSURLResponse?, NSError?) -> Void

Some unwrapping going wrong? Please help me out.

Thanks.

BTW: I am using the latest version of Xcode/Swift

Tukang Keboen
Tukang Keboen
2,840 Points

since swift 2: ! change to ?

try this

 (request, completionHandler: { (url: NSURL?, response: NSURLResponse?, error: NSError?) -> Void in} )