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

NSData error when making network call in Weather App

Hi, I followed the Teacher's Note to update the NSData code, but I keep getting an error that says, "fatal error: unexpectedly found nil while unwrapping an Optional value". What's wrong with my code below?

override func viewDidLoad() {
    super.viewDidLoad()

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

    let forecastURL = NSURL(string: "37.784296,-122.400006", relativeToURL: baseURL)

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

    println(weatherData)


}

1 Answer

in the world of computer programming, when a object reference, or data reference returns "nil", or "null", usually means the reference object (aka the NSData object variable) isn't pointing to anything but in swift, a "nil" reference means there is an absence of a value. I would check to make sure the link provided in the tutorial is valid in the web browser. Also, make sure you know exactly what optionals do. Optionals can prevent a nil reference from occurring during compile time.

Please refer to this link for more information about optionals

https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/OptionalChaining.html