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

Vale of optional type 'NSURL?' not unwrapped; did you mean to use '!' or '?'?

Here is my code I'm currently on Xcode 6.1 Yosemite and I'm have trouble understanding why I'm getting this error every time I try to run the app.

let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
        let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)

        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: <NSURL!>, response: <NSURLResponse!>, error: <NSError!>) -> Void in
            println(response)
        })

    }

4 Answers

Hi Frank,

This was an change that was made to Xcode 6.1, what it's asking you to do is add a bang or exclamation mark after forecastUrl on the line where you've declared downloadTask.

For example.

let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
    println(response)
})

Hi guys I'm reviewing this course. I've gone and added the bang to forecastURL

let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
            println(response)

when I do so I get Expected perimeter type following ':' error when running the app.

Hi Frank,

Could you please post the rest of your code minus your API key.

Cheers.

Here it is.

 let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")

        let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)

        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
            println(response)
        })
        downloadTask.resume()


    }

I ended up figuring it out from this thread Please help with coding error/issue. Elijah Gartin's answer fixed my issue. Did not realize that I had to get rid of the blue boxes.