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 Using Our JSON Data

Value of optional type NSURL? not unwrapped - Swift weather app issue

On line 23, it shows an error for the forcastURL argument (inside the downloadTask line). I get: "Value of optional type NSURL? not unwrapped: did you mean to use '!' or '?' "

I even downloaded the finished project (line 42) and I have the same issue - how do you fix this? I'm on Yosemite and Xcode 6.1

P.S. I looked at the other question, and there's no space in the Lat,Long.

3 Answers

Joakim Birger Lie
Joakim Birger Lie
12,367 Points

I had the exact same problem. The fix turned out to be a difference in the syntax for NSData.

Instead of the suggested:

let weatherData = NSData.dataWithContentsOfURL(forecastURL, options: nil, error: nil)

Write:

let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)

Stone Preston
Stone Preston
42,016 Points

thats because the videos are using xcode 6, not xcode 6.1. Some things in swift have changed in 6.1. you probably need to force unwrap the NSURL variable by placing a bang (!) after the variable name

even with the bang it will build successful but a nil error will show up in the debug console

Stone Preston
Stone Preston
42,016 Points

can you post the relevant code you are using like the line where you actually set the NSURL object

Cesar Aviles
Cesar Aviles
3,998 Points

This builds for me but my response is nil in the output window, it's hello nil. I put the hello to confirm that's where the nil was coming from.

private let apiKey = "04d038cd665f17fa7727f70c7a5f0d3f"

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 weatherData = NSData(contentsOfURL: forecastUrl!, options: nil, error: nil)
    println("hello \(weatherData)")

}