Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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)


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

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.