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

Swift: Weather App - Making a Network Call not connecting to API to return data

I have followed step by step as the instructor has in the whether app.

http://teamtreehouse.com/library/build-a-weather-app-with-swift/pulling-data-from-the-web/making-a-network-call

I receive no error and the app runs. However, the console returns nil. The first time I ran the app it returned the data from the API and Forecast was updated and showed I made a call. I have not been able to make a call again. Here is my code.

import UIKit

class ViewController: UIViewController {

    private let apiKey = "6638a13657b81ecbe08a47749ecfa9b7"

    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: "32.302452,-80.975017", relativeToURL: baseURL)

        let weatherData = NSData.dataWithContentsOfURL(forecastURL, options: nil, error: nil)
        println(weatherData)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

1 Answer

Try changing the URL from

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

to

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

Thanks, such a minor thing.