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

Nathan Wilkins
Nathan Wilkins
4,554 Points

When running the app it returns nil

I have looked through some of the previous questions and answers and looking at my code I can't see what is wrong with it.

When running it in a playground it still returns nil but looking at the Console Output there seems to be an issue in that the HTTP load failed.

private let apiKey = "2d4c1bdf29ae4484a321809f17582e8a"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


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

    let forecastURL = NSURL(string: "51.620345,0.045681", relativeToURL: baseURL)

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

    println(weatherData)
}

Any ideas?

2 Answers

Stone Preston
Stone Preston
42,016 Points

the forecast API docs state that an api call looks like this:

https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE

as you can see, your base URL is:

"https://api.forecast.io/\(apiKey)/"

you are missing the /forecast/ part of the URL. add that into your base URL:

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

Thank you Stone. I had the same exact issue.

Nathan Wilkins
Nathan Wilkins
4,554 Points

Whoa man. Thanks Stone.

I knew it would be something silly like that! I did have it working earlier, I must have removed it at some point.

Thanks again.

Stone Preston
Stone Preston
42,016 Points

no problem glad I could help