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

weather app keeps returning nil from weatherData

It keeps returning nil for my weatherData.

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)

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

Jamie,

Are you using your own API Key? Make sure it's not the one from the video.

Yes, I'm using my own api key, but still no luck.

Can you post the rest of your code for us to look at.

1 Answer

try this instead

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

NSURL now returns an optional and needs to be unwrapped.

Philip Williams
Philip Williams
8,708 Points

Hello,

I have tried this solution but it still returns an error. Has anyone got these lines of code to return anything other than "nil"?