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 (Retired) Concurrency Writing Concurrent Networking Code

George Rodriguez
George Rodriguez
1,681 Points

value of optional type 'NSURL?' not unwrapped, did you mean to use '!' or '?'

I am Currently running Yosemite 10.10 (14A379a) GM and Xcode Version 6.1 (6A1042b) GM Seed.

This is my code:

import UIKit
class ViewController: UIViewController {
    private let apiKey = " PRIVATE"
    override func viewDidLoad() {
        super.viewDidLoad()
        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
        let forecastURL = NSURL(string: "25.705042, -80.429967", relativeToURL: baseURL)
        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
            println(response, error)
        })
        downloadTask.resume()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

I keep getting : value of optional type 'NSURL?' not unwrapped, did you mean to use '!' or '?'

I have tried using a the public release of Xcode 6.0.1 and still have issues.

Has anyone found a workaround. I really want to get back on track I have searched all around I know its an Xcode issue but I wanted to know if anyone is experiencing the same thing?

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi George,

You have a space in your lat,lng value which won't work as NSURL doesn't expect spaces, simply remove the space like I've done below and you code should work fine after that.

let forecastURL = NSURL(string: "25.705042,-80.429967", relativeToURL: baseURL)
George Rodriguez
George Rodriguez
1,681 Points

Hey Chris,

When I read you're reply I started laughing because I am always messing up with spaces in my code in Ruby. I can't even tell you how many times this has happened. Thank you so much for your reply. I just tried it and it worked and returns a response.

Chris Shaw
Chris Shaw
26,676 Points

No worries, it's always the simple oversights that can cause the strangest problems.

I have the same issue - but I don't have a space in the Lat,Long.

Chris Shaw
Chris Shaw
26,676 Points

M T, could you please create a new thread for your issues, thanks.

I Have the same problem here

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()