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) Data Modeling With Structures Cleaning Up Our Date

fatal error: unexpectedly found nil while unwrapping an Optional value

get this error and I thought it might have to do with unwrapping the dataObject.. but still I cannot figure it out. (JSON method is slightly different due to the new version of XCODE)

viewcontroller.swift

  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: "48.189250,16.390753", relativeToURL: baseURL)
        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: {(location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

            if error == nil {
                let dataObject = NSData(contentsOfURL: location)
                let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
                let currentWeather = Current(weatherDictionary: weatherDictionary)
                println(currentWeather.temperature)
            }
        })
        downloadTask.resume()
    }

Current.swift

struct Current {

    var currentTime: Int
    var temperature: Int
    var humidity: Double
    var precipProbability: Double
    var summary: String
    var icon: String

    init(weatherDictionary: NSDictionary) {

        let currentWeather = weatherDictionary["current"] as NSDictionary

        currentTime = currentWeather["time"] as Int
        temperature = currentWeather["temperature"] as Int
        humidity = currentWeather["humidity"] as Double
        precipProbability = currentWeather["precipProbability"] as Double
        summary = weatherDictionary["summary"] as String
        icon = weatherDictionary["icon"] as String

    }

}

I did find the error... it should read weatherDictionary["currently"] instead of "current" Furthermore I later falsely use weatherDictionary instead of currentWeather... what was I thinking.

3 Answers

Stone Preston
Stone Preston
42,016 Points

first lets make sure dataObject is not nill.

print out the value of dataObject before unwrapping so you can see if its nil:

if error == nil {
                let dataObject = NSData(contentsOfURL: location)
                 println(dataObject)
                let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
                let currentWeather = Current(weatherDictionary: weatherDictionary)
                println(currentWeather.temperature)
            }

if its nil thats the variable thats causing the problem,

Thanks for your answer. The data object is not nil, but contains the unwrapped information.

how do i fix the variable problem if i am getting a nil still?

Stone Preston
Stone Preston
42,016 Points

can you post your code