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

building a weather App

After doing all the things that we did in the video from "Adding a Refresh Button" when i run my code a get a "terminating with uncaught exception of type NSException" in my console. Don't know what I did wrong??

import UIKit

class ViewController: UIViewController { private let apiKey = "7919218f8bf1fbe2a815b1606da1535b"

@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!
@IBOutlet weak var refreshButton: UIButton!
@IBOutlet weak var refreshActivityIndicator: UIActivityIndicatorView!

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

    getCurrentWeatherData()
}

func getCurrentWeatherData() -> Void {
    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    let forecastURL = NSURL(string: "37.8267,-122.423", 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)"

                //Stop refresh animation
                self.refreshActivityIndicator.stopAnimating()
                self.refreshActivityIndicator.hidden = true
                self.refreshButton.hidden = true
            })

        }

    })

    downloadTask.resume()
}


@IBAction func refresh() {
    getCurrentWeatherData()

    refreshButton.hidden = true
    refreshActivityIndicator.hidden = false
    refreshActivityIndicator.startAnimating()
}

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

}

Another Note when i try to debug it it won't pass the AppDelegate file

2 Answers

ok go to your view controller (the code) and remove the outlet for your refreshActivityIndicator. just delete this line of code.

@IBOutlet weak var refreshActivityIndicator: UIActivityIndicatorView!

then go to your storyboard file. click on the view controller and open up the connections inspector (its in the utlities pane, its the button that looks like a circle with an arrow in it). delete the connection for the activityIndicator. then open the assistant editor. control click and drag from the activityIndicator on your storyboard to the file to connect them again

what happened was you connected the acitivity indicator and named it. then you changed the name and this caused the connection to the storyboard to mess up and you get this error

yes thats true thanks I got it to work now i just have figured out why my refresh button does not show when i run my app. but thank you again

what does the console say about the exception. if you scroll down it should say exactly whats causing the exception to be thrown

This correct??

2014-12-02 16:41:34.852 Stormy[895:25774] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Stormy.ViewController 0x7f8449964ed0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key refreshActivityIndicatior.'