swiftruntimenstimernsunknownkeyexception

NSUnknownKeyException this class is not key value coding-compliant for the key


So I am working through a tutorial and I have made sure that I followed the author to the letter and my code throws the following error.

2014-10-01 22:26:14.545 stopwatch2[3503:861733] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<stopwatch2.ViewController 0x7c57f050> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key pause.'

My Question is:

Based on the error provided what is the reason my application is failing ?

Here's my code that won't compile:

import UIKit

class ViewController: UIViewController {

    var timer = NSTimer()

    @IBOutlet var time : UILabel!
    var count = 0

    @IBAction func play(sender : AnyObject) {

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
    }

    @IBAction func pause(sender : AnyObject) {
        timer.invalidate()
    }


    @IBAction func reset(sender : AnyObject) {
        timer.invalidate()
        count = 0
        time.text="0"
    }



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



    }

    func result() {
        count++
        time.text = String(count)
    }

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


}

What I would really like to know, is how to investigate this on my own because I am sure the authors other videos will yield the same results.

The videos are from udemy.com, The Complete iOS8 and Swift Course..

Thanks in advance for any help.


Solution

  • "this class is not key value coding-compliant for the key pause" usually means you have a referencing outlet problem. Look in the Connections Inspector for your different buttons. You may likely either have:

    2 referencing outlets for one button and the program does not know which ne to use, etc.

    I ran the code hooking up 1 label to an IBOutlet and three buttons (play, pause, reset), one to each IBAction, and it ran perfectly.