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 (Retired) Pulling Data From the Web Making a Network Call

Blake Hudson
Blake Hudson
3,817 Points

Shouldn't This Be Working!?! HELP!!!!

Ive refreshed the api Key.... I dont understand why this code doesnt work.... please make me feel stupid if you could! Thanks!

 private let apiKey = "ea8492f487ebe0e498ab1b1ec3ab0663"

    override func viewDidLoad() {
        super.viewDidLoad()

        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!, options: nil, error: nil)

       println(weatherData)

1 Answer

Dominic Bryan
Dominic Bryan
14,452 Points

Okay I am guna advise re-checking your API key. Your code is fine but when I tried using the api key you are using (Which by the way is private and you shouldn't post on a forum LOL but its for practice I guess ahaha) "nil" was returned. I will post code below, but try changing your api key and let me know what happens.

private let apiKey = "ea8492f487ebe0e498ab1b1ec3ab0663"

 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/forecast/\(apiKey)/")
        let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)

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

P.S. the api key in the code I have posted is yours, I just copied it in for you so you can copy my code

Thanks, Hope this helps

Dom

Alfred Angkasa
Alfred Angkasa
2,413 Points

I also did the same error after i read this now i understand. thanks. :D