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

Andy Williams
Andy Williams
4,436 Points

wetherData is nil


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.


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)/")


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)
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 ;)