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

Justin Estrada
Justin Estrada
34,995 Points

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) ive been stuck for a couple hours

I know this is a pretty commomn problem due to misspellings and stuff, i am just extremely stuck

the line that is highlighted green and displays "exc_bad_instruction (code=exc_i386_invop subcode=0x0)" is :

currentTime = currentWeather["currentTime"] as Int

Justin Estrada
Justin Estrada
34,995 Points

// Current.swift file

import Foundation

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["currently"] as NSDictionary

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

}

}

Justin Estrada
Justin Estrada
34,995 Points

// // ViewController.swift

import UIKit

class ViewController: UIViewController {

private let apiKey = "c6ca594eab4a9053b9bf02212b474537"

override func viewDidLoad() {
    super.viewDidLoad()

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

    let forecastURL = NSURL(string: "56.247928,-120.836373/", 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()

Dude! If you're still having trouble with this, I fixed the problem by double checking my API key. At one point I copied the instructor's code to fix a different problem, and I forgot to replace his API key with mine! So the download failed and I got a nil object at the downloadTask stage!