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


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