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

Im getting an issue with my dataObject and my Current.swift file

here is my code:

Current File: import Foundation

struct Current {

// Make them optional so you dont have to initialize since we cant make up defaults
var currentTime: Int
var tempurature: Int
var humidity: Double
var precipProbability: Double
var summary: String
var icon: String

init(weatherDictionary: NSDictionary) {
    // Get Current Weather
    let currentWeather = weatherDictionary["currently"] as NSDictionary

    currentTime = currentWeather["time"] as Int
    tempurature = currentWeather["tempurature"] as Int
    humidity = currentWeather["humidty"] as Double
    precipProbability = currentWeather["precipProbability"] as Double
    summary = currentWeather["summary"] as String
    icon = currentWeather["icon"] as String

}

}

ViewController:

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

    // Set up API URL (Get this from the API website)
    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")

    // Set up coordinates - if you would like you could store in a struct property and call from the array
    let forecastURL = NSURL(string: "56.247928,-120.836373", relativeToURL: baseURL)


    // Make a data call
    let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)

    let sharedSession = NSURLSession.sharedSession()



   // Download data

    let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

        // Check to see if there are any errors
        // print and utilize JSON data  and put it into a structured dictionary

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.humidity)

        }


    })

    downloadTask.resume()

}
Stone Preston
Stone Preston
42,016 Points

what specific error are you getting and on what line

5 Answers

Stone Preston
Stone Preston
42,016 Points

you misspelled temperature so the key is incorrect:

/// there is no key "tempurature", it needs to be "temperature"
tempurature = currentWeather["tempurature"] as Int

try fixing the key

tempurature = currentWeather["temperature"] as Int

you should probably go back and change everywhere with the incorrect spelling to be the correct one:

var currentTime: Int
// fixed it here
var temperature: Int
var humidity: Double
var precipProbability: Double
var summary: String
var icon: String

init(weatherDictionary: NSDictionary) {
    // Get Current Weather
    let currentWeather = weatherDictionary["currently"] as NSDictionary

    currentTime = currentWeather["time"] as Int
    // and here
    temperature = currentWeather["temperature"] as Int
    humidity = currentWeather["humidty"] as Double
    precipProbability = currentWeather["precipProbability"] as Double
    summary = currentWeather["summary"] as String
    icon = currentWeather["icon"] as String

}

The Build Succeeds and then I'm getting this in the console along with an array of nnumbers: fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

then the debug window pops up in the Current.swift file and the temperature = currentWeather["tempurature"] as Int line reads "Thread 6: EXC_BAD_INSTRUCTION

thanks for this..I actually tried that already and im still getting in the console something like this.

... 5b223731 30353631 2d393939 3939222c 22373130 3536332d 39393939 39222c22 37313232 31302d39 39393939 222c2237 31343731 302d3939 39393922 2c223731 39343330 2d393939 3939225d 2c226d61 6469732d 73746174 696f6e73 223a5b22 4359584a 225d2c22 756e6974 73223a22 7573227d 7d>) 3:50 PM

I have followed along with the Cleaning up our date video so I was trying to print the time

Stone Preston
Stone Preston
42,016 Points

thats the data object being printed. remove the line that prints the dataObject:

if (error == nil) {
  let dataObject = NSData(contentsOfURL: location)
   //remove this line below
   println(dataObject)

awesome thanks!

humidity is spelled wrong