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

Richard Price
Richard Price
2,050 Points

returning NIL from api call

any ideas API is 100 percent correct

private let apiKey = "bf4be97c0804c2917bf9ae23575a6679"

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)


}
Dan Merchant
Dan Merchant
Courses Plus Student 22,186 Points

I'm having trouble with that too. When I println(forecastURL) it looks backwards, try it yourself to see if the url is correct.

to fix it I just hard coded the URL with a String.

It's early days for Xcode 6 and Swift and the tutorials for swift, so something isn't working correctly.

3 Answers

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

I copied your code and it does not work! Getting a compiler error "bound value in a conditional binding must be of optional type"

Here's the problem:

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

The example actually shows:

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

This has one more backslash for the url. No idea why this would be needed, since the API does show it sans trailing backslash.

Richard Price
Richard Price
2,050 Points

cheers got it working with this code

private let apiKey = "bf4be97c0804c2917bf9ae23575a6679" private let locationPoints = "37.8267, -122.423"

override func viewDidLoad() {

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

    let baseURL = "https://api.forecast.io/forecast/"
    let encodedPoints = locationPoints.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! // This method returns an optional, we need to force unwrap

    if let forecastURL = NSURL(string: "\(baseURL)\(apiKey)/\(encodedPoints)") { // We have to optionally bind because this method returns an optional and we need to act on the value it returns.

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

        println(weatherData)

    }

}