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) Pulling Data From the Web Making a Network Call

Tom Hill
Tom Hill
14,232 Points

Stormy new NSData format optional issue

Hello, I'm having an issue with the Build a Weather App in Swift - Making a Network Call video where Xcode is returning an error:

fatal error: unexpectedly found nil while unwrapping an Optional value

and it's happening because of this line:

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

that was updated in the 6.1 version of Xcode which I replaced for the original line like as the teacher's notes instructed.

I might just be me doing something wrong (it probably is) however any help would be appreciated!

Thanks, Tom Hill

P.S my entire code it the viewDidLoad method looks like this:

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

4 Answers

Stone Preston
Stone Preston
42,016 Points

I dont really see anything wrong with your code, so print out the URL and verify that its nil:

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)
    println("URL: \(forecastURL)")
    let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
    println(weatherData)

if it indeed comes back as nil double check your API key

Tom Hill
Tom Hill
14,232 Points

Ha... that's really weird - as soon as I add that println, it works - then I take it out again, still works... strange.

Anywho - that fixed it!

Thank you!

Stone Preston
Stone Preston
42,016 Points

strange, might have been an issue with forecast or something. maybe it took a while for your API key to go through or something i dont know. oh well.

Michael Baker
Michael Baker
5,709 Points

Had same issue - removing the space after the comma in the latlong data did the trick.

Bruno Paes Leme
Bruno Paes Leme
1,796 Points

I have the same issue. Even after trying the same as you did, I still get the same problem. Anything else that could be cause the problem?

Colton Ballinger
Colton Ballinger
1,767 Points

I had the same problem as well. However, check to make sure the forecastURL coordinates have no space in between the commas.

For example, below is code that was causing the compile error before I fixed it. Notice the space between 37.8267 and -122.423. I just had to close that space and the problem was resolved.

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

Anand P
Anand P
893 Points

Tom Hill, it is really weird, I put the println fpr URL and bang, that made it bring back data instead of nil.