Dan*_*ieu 1 runtime nstimer swift nsunknownkeyexception
因此,我正在完成一个教程,并且我已确保我严格按照作者的要求进行操作,并且我的代码会抛出以下错误。
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.'
Run Code Online (Sandbox Code Playgroud)
我的问题是:
根据提供的错误,我的应用程序失败的原因是什么?
这是我的无法编译的代码:
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.
}
}
Run Code Online (Sandbox Code Playgroud)
我真正想知道的是如何自己调查这个问题,因为我确信作者的其他视频也会产生相同的结果。
这些视频来自 udemy.com,完整的 iOS8 和 Swift 课程。
预先感谢您的任何帮助。
“此类与键暂停的键值编码不兼容”通常意味着您有引用插座问题。在连接检查器中查找不同的按钮。您可能有:
一键引用 2 个插座,程序不知道要使用哪个插座,等等。
我运行了代码,将 1 个标签连接到 IBOutlet 和三个按钮(播放、暂停、重置),每个 IBAction 一个,并且运行完美。