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.


Ludwing Najera
Ludwing Najera
4,596 Points

My forecastURL is nil?

In my ViewDidLoad, my weatherData object is giving me a fatal error when i run the app. Is says my forecastURL object is nil, or like what the console said, "fatal error: unexpectedly found nil while unwrapping an Optional value." Is it because I'm force unwrapping the forecastURL? Here's my code.

private let forecastAPIKey = "51c9c3a1c89948f84fb48ca5db50fdda"

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

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/")
    let forecastURL = NSURL(string: "39.424031, -77.407157", relativeToURL: baseURL)

    // Data object to fetch weather data
    //this is where I get my fatal error 
    let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Ludwing Najera,

You have to remove the space between the longitude and latitude values. Theres no space in the URL.

let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/")
// Remove the space between the longitude and latitude values
let forecastURL = NSURL(string: "39.424031,-77.407157", relativeToURL: baseURL)

Good Luck