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

My api key is correct and all my code is identical. Why am I getting nil?

My code for the stormy app is just returning nil. I have check and rechecked my apiKey. I don't have spaces in the url. I am not crashing. I am just printing out nil.

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

    let forecastURL = NSURL(string:"43.650165,-116.279280", relativeToURL: baseURL)

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

    println(weatherData)

Everyone. It was a network problem. It started working without any changes.

wachter
wachter
1,594 Points

I have the exact same code as you and I am also getting nil. However my network is fully working so that shouldn't be the problem. What could it be?

1 Answer

I didn't change anything, and it has been working fine now. Can't explain it. I think it had to do with something on their end. I have been testing it for weeks (since I posted this) and it hasn't quit yet. My app is in review right now. I used this tutorial to add a weather feature to an event app I wrote. If your running in the simulator, turn your wifi off and back on and try to run the app. See if it still gets nil.