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 Interacting with Data From the Web Writing Concurrent Networking Code

NSURLRequest Object

With the following code I'm getting the following error message:

// NSURLRequest Object
        let request = NSURLRequest(URL: forecastURL!)

        let dataTask = session.dataTaskWithRequest(request, completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
        })

        dataTask.resume()

error: Cannot convert value of type '(NSData!, response: NSURLResponse!, error: NSError!) -> Void' to expected argument type '(NSData?, response: NSURLResponse?, error: NSError?) -> Void'

I am using Xcode 7.2.1 and Swift 2.1

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey JP Tomberlin,

Your method signature doesn't match the expected argument for dataTaskWithRequest. Try making the parameters optionals instead of ImplicitlyUnwrappedOptionals.

I believe the signature you are using is outdated and they have been changed to optionals.

// NSURLRequest Object
        let request = NSURLRequest(URL: forecastURL!)

        let dataTask = session.dataTaskWithRequest(request, completionHandler: {(data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
        })

        dataTask.resume()

Good Luck

Martin Franzner
Martin Franzner
3,300 Points

Hey JP Tomberlin,

Ok, it worked, the message "Cannot convert value of type..." has gone, but when I put to run the app now, in the line of the 'let request = NSURLRequest(URL: forecastURL!)'

it returns: "fatal error: unexpectedly found nil while unwrapping an Optional value" And a message in red saying EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,subcode=0x0)

Any idea what is happening?