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

Problem with making API call for "Stormy"

There was a change in the code and I entered the new code as instructed. However I am getting an error that says I am returning a nil in an unwrapped optional. Since this code was in the teacher's notes and not the video there is no instruction. I don't know how to proceed.

import UIKit

class ViewController: UIViewController {

private let apiKey = "3e04d92db5835cf57669dcf845ce0f27"

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: "43.064117, -70.762250", relativeToURL: baseURL)

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

    println(weatherData)


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

2 Answers

Stone Preston
Stone Preston
42,016 Points

remove the space between the coordinates in your url:

right now you have a space after the comma of your first coordinate:

let forecastURL = NSURL(string: "43.064117, -70.762250", relativeToURL: baseURL)

remove the space:

let forecastURL = NSURL(string: "43.064117,-70.762250", relativeToURL: baseURL)

URLs cannot have spaces in them, so thats probably why you were getting nil

Bruno Paes Leme
Bruno Paes Leme
1,796 Points

Great stuff man, thanks a lot.

Thank you, that took care of it.

Stone Preston
Stone Preston
42,016 Points

cool glad that fixed it