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

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() {
    // 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)


    override func 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