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

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