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 Interacting with Data From the Web Fetching Data

Andy Williams
Andy Williams
4,436 Points

wetherData is nil

Hi

My weatherData constant contains nil. I have checked the URL and APIKey and all is ok. I made sure the Simulator can access the internet by checking Safari.

Not sure what else I can try.

Thanks

Artem Sherbachuk
Artem Sherbachuk
2,161 Points

I have the same first check all check the url in browser if server respond "JSON" if all is ok then check code in my case I was not attentive I put two times the key check Backslash "/" after interpolation (apiKey)/

2 Answers

I also ran into the same issue and banged my head up against this for two whole days. I ran this in a playground and found that my error came from a missing forward slash in the base URL.

I figured out this problem by typing each line one by one in the playground. As you create your baseURLs and forcastURLs, the editor will show you the result as you create it.

You should wind up with something like this

import UIKit

private let forecastAPIKey = "YOUR API KEY GOES HERE"


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

println(baseURL)

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

// Data object to fetch weather data. NSData performs a network call when used
let weatherData = NSData (contentsOfURL: forecastURL!, options: nil, error: nil)
println(weatherData)
Girri M Palaniyapan
Girri M Palaniyapan
7,829 Points

Awesome folks! This was helpful. I had some unnecessary white space inside the string. Big take away: Be careful with white space especially with strings which are used as links.

michael aune
michael aune
2,333 Points

thanks for the assist! duly noted on the attention to detail needed on the URL ;)