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) Displaying Our Weather Data Connecting the UI to Our Model

getting error in xcode 6.1 final on Yosimite

This is my compiler error:

2014-10-21 23:01:10.762 Stormy[5602:3546829] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Stormy.ViewController 0x7f8b717814c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key temperatureLable.' *** First throw call stack: ( 0 CoreFoundation 0x000000010b4bef35 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010d002bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010b4beb79 -[NSException raise] + 9 3 Foundation 0x000000010b8d67b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259 4 CoreFoundation 0x000000010b408e80 -[NSArray makeObjectsPerformSelector:] + 224 5 UIKit 0x000000010c00fc7d -[UINib instantiateWithOwner:options:] + 1506 6 UIKit 0x000000010be6ef98 -[UIViewController _loadViewFromNibNamed:bundle:] + 242 7 UIKit 0x000000010be6f588 -[UIViewController loadView] + 109 8 UIKit 0x000000010be6f7f9 -[UIViewController loadViewIfRequired] + 75 9 UIKit 0x000000010be6fc8e -[UIViewController view] + 27 10 UIKit 0x000000010bd8eca9 -[UIWindow addRootViewControllerViewIfPossible] + 58 11 UIKit 0x000000010bd8f041 -[UIWindow _setHidden:forced:] + 247 12 UIKit 0x000000010bd9b72c -[UIWindow makeKeyAndVisible] + 42 13 UIKit 0x000000010bd46061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628 14 UIKit 0x000000010bd48d2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350 15 UIKit 0x000000010bd47bf2 -[UIApplication workspaceDidEndTransaction:] + 179 16 FrontBoardServices 0x000000010eb8f2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16 17 CoreFoundation 0x000000010b3f453c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 12 18 CoreFoundation 0x000000010b3ea285 __CFRunLoopDoBlocks + 341 19 CoreFoundation 0x000000010b3ea045 __CFRunLoopRun + 2389 20 CoreFoundation 0x000000010b3e9486 CFRunLoopRunSpecific + 470 21 UIKit 0x000000010bd47669 -[UIApplication _run] + 413 22 UIKit 0x000000010bd4a420 UIApplicationMain + 1282 23 Stormy 0x000000010b2d882e top_level_code + 78 24 Stormy 0x000000010b2d886a main + 42 25 libdyld.dylib 0x000000010d7dc145 start + 1 26 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Andres Oliva
Andres Oliva
7,810 Points

Can you post your code?

{

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var iconView: UIImageView!
@IBOutlet weak var currentTimeLabel: UILabel!
@IBOutlet weak var temperatureLabel: UILabel!
@IBOutlet weak var humidityLabel: UILabel!
@IBOutlet weak var precipitationLabel: UILabel!
@IBOutlet weak var summaryLabel: UILabel!


    // privately stores the forecast.io api key
private let apiKey = "86d7a55107698f774f17ca8b0fad7ec6"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    // My Personal Lat/long location: 43.034559, -89.389465
    // let forecastURL = "37.8267,-122.423"

        /* the baseURL stores the main, resusable portion of the url
        *  and inserts the private apiKey into the URL along with
        *  Along with the final portion in the forecastURL
        *  the lat/long would also be in its own variable in a real
        *  world application.
        */

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    let forecastURL = NSURL(string: "43.034559,-89.389465", 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)

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.temperatureLabel.text = "\(currentWeather.temperature)"
                self.iconView.image = currentWeather.icon!
                self.currentTimeLabel.text = "At \(currentWeather.currentTime!) it is"
                self.humidityLabel.text = "\(currentWeather.humidity)"
                self.precipitationLabel.text = "\(currentWeather.precipProbability)"
                self.summaryLabel.text = "\(currentWeather.summary)"
            })
        }

    })
    downloadTask.resume()

    //let weatherData = NSData(contentsOfURL: forecastURL!)
    //println(weatherData)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

current.swift

<code> // // current.swift // Stormy // // Created by Dan Beaulieu on 10/21/14. // Copyright (c) 2014 Dan Beaulieu. All rights reserved. //

import Foundation import UIKit

struct Current {

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

init(weatherDictionary: NSDictionary) {
    let currentWeather = weatherDictionary["currently"] as NSDictionary

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

    let currentTimeIntValue = currentWeather["time"] as Int
    currentTime = dateStringFromUnixtime(currentTimeIntValue)

    let iconString = currentWeather["icon"] as String
    icon = weatherIconFromString(iconString)
}

func dateStringFromUnixtime(unixTime: Int) -> String {
    let timeInSeconds = NSTimeInterval(unixTime)
    let weatherDate = NSDate(timeIntervalSince1970: timeInSeconds)

    let dateFormatter = NSDateFormatter()
    dateFormatter.timeStyle = .ShortStyle

    return dateFormatter.stringFromDate(weatherDate)
}

func weatherIconFromString(stringIcon: String) ->  UIImage {
    var imageName: String

    switch stringIcon {
        case "clear-day":
            imageName = "clear-day"
        case "clear-night":
            imageName = "clear-night"
        case "rain":
            imageName = "rain"
        case "snow":
            imageName = "snow"
        case "sleet":
            imageName = "sleet"
        case "wind":
            imageName = "wind"
        case "fog":
            imageName = "fog"
        case "cloudy":
            imageName = "cloudy"
        case "partly-cloudy-day":
            imageName = "partly-cloudy"
        case "partly-cloudy-night":
            imageName = "cloudy-night"
        default:
            imageName = "default"

    }

    var iconImage = UIImage(named: imageName)
    return iconImage!
}

} </code>

1 Answer

go to Main.storyboard - > View controller and see all the Outlets. There must be some outlets that are not connected with any button. Probably you misstyped and re-added the UILabel button in the ViewController.swift file.

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key temperatureLable.' ***

from the exception i am sure you didnt notice the mistyped Label -> "temperatureLable" and in code you have temperatureLabel