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

NSURL Optional in Weather App

I'm coding up a weather app along side Pasan in one of the iOS tracks. As far as I can tell my code matches his however I'm having a problem with my NSURL being an optional where his is not. Here is my code.

class ViewController: UIViewController {

    private let apiKey = "92a0b81218fc51a38a717d1aed2b4a22"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        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: {
            (NSURL!, NSURLResponse!, NSError!) -> Void in

        })

    }

It's returning forecastURL as an optional and then giving me an error in my download task constant because it says I need to unwrap forecastURL. I need help understanding whats going on here and how to fix it.

Thanks.

Here is the code a bit further along following the video.

class ViewController: UIViewController {

    private let apiKey = "92a0b81218fc51a38a717d1aed2b4a22"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

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

        let unwrappedForecastURL = forecastURL!

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

    }

It's functioning properly. I just unwrapped forecastURL. I would still like to know why it was an optional in the first place.

2 Answers

Hi Jonathan,

This isn't an issue with the course per say but an unforeseen update that took place in Xcode 6.1, as of 6.1 NSURL objects are now optionals so we can evaluate them properly through IF LET statements if the URL given couldn't be parsed whereas before this could have thrown an exception and caused a crash.

Wonderful, thanks.