Swift 秒表应用程序崩溃,不知道我哪里出错了
Swift Stopwatch App crashing, don't know where I'm going wrong
我正在尝试为我正在学习的编码制作一个秒表应用程序 class。在我 运行 代码之前我没有错误,但是一旦我 运行 它在 "build succeeds" 之后崩溃了。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var timer = NSTimer()
var count = 0
func updateTime() {
count++
timeLabel.text = "\(count)"
}
@IBOutlet weak var timeLabel: UILabel!
@IBAction func playButton(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
@IBAction func pauseButton(sender: AnyObject) {
}
@IBAction func resetButton(sender: AnyObject) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是输出控制台:
2015-07-23 12:52:26.877 Stop Watch[7472:467329] *** Terminating app
due to uncaught exception 'NSUnknownKeyException', reason:
'[<Stop_Watch.ViewController 0x7ffdf1435b60> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key play.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a814c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c37fbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010a8148a9 -[NSException raise] + 9
3 Foundation 0x000000010ac32b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x000000010a75cd50 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x000000010b38b4eb -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x000000010b1e36d8 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x000000010b1e3cc8 -[UIViewController loadView] + 109
8 UIKit 0x000000010b1e3f39 -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x000000010b1e43ce -[UIViewController view] + 27
10 UIKit 0x000000010b0ff289 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010b0ff64f -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010b10bde1 -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010b0af417 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
14 UIKit 0x000000010b0b219e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
15 UIKit 0x000000010b0b1095 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x000000010de7c5e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
17 CoreFoundation 0x000000010a74841c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x000000010a73e165 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x000000010a73df25 __CFRunLoopRun + 2389
20 CoreFoundation 0x000000010a73d366 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010b0b0b02 -[UIApplication _run] + 413
22 UIKit 0x000000010b0b38c0 UIApplicationMain + 1282
23 Stop Watch 0x000000010a618fa7 main + 135
24 libdyld.dylib 0x000000010cab5145 start + 1
25 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
查看控制台日志后,我很确定崩溃是由于缺少插座连接引起的。
按 Cmd+Shift+F 并在搜索字段中键入 'play'。
在故事板文件中查找 ...: Outlet = "play"
.
形式的条目
单击该条目并通过单击 play
旁边的 'x' 按钮将其删除。
这应该可以解决崩溃问题。
每当您看到以下形式的错误时:
... this class is not key value coding-compliant for the key ...
你应该检查你的故事板或 xib 中是否缺少插座。
我正在尝试为我正在学习的编码制作一个秒表应用程序 class。在我 运行 代码之前我没有错误,但是一旦我 运行 它在 "build succeeds" 之后崩溃了。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var timer = NSTimer()
var count = 0
func updateTime() {
count++
timeLabel.text = "\(count)"
}
@IBOutlet weak var timeLabel: UILabel!
@IBAction func playButton(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
@IBAction func pauseButton(sender: AnyObject) {
}
@IBAction func resetButton(sender: AnyObject) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是输出控制台:
2015-07-23 12:52:26.877 Stop Watch[7472:467329] *** Terminating app
due to uncaught exception 'NSUnknownKeyException', reason:
'[<Stop_Watch.ViewController 0x7ffdf1435b60> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key play.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a814c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c37fbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010a8148a9 -[NSException raise] + 9
3 Foundation 0x000000010ac32b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x000000010a75cd50 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x000000010b38b4eb -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x000000010b1e36d8 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x000000010b1e3cc8 -[UIViewController loadView] + 109
8 UIKit 0x000000010b1e3f39 -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x000000010b1e43ce -[UIViewController view] + 27
10 UIKit 0x000000010b0ff289 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010b0ff64f -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010b10bde1 -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010b0af417 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
14 UIKit 0x000000010b0b219e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
15 UIKit 0x000000010b0b1095 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x000000010de7c5e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
17 CoreFoundation 0x000000010a74841c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x000000010a73e165 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x000000010a73df25 __CFRunLoopRun + 2389
20 CoreFoundation 0x000000010a73d366 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010b0b0b02 -[UIApplication _run] + 413
22 UIKit 0x000000010b0b38c0 UIApplicationMain + 1282
23 Stop Watch 0x000000010a618fa7 main + 135
24 libdyld.dylib 0x000000010cab5145 start + 1
25 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
查看控制台日志后,我很确定崩溃是由于缺少插座连接引起的。
按 Cmd+Shift+F 并在搜索字段中键入 'play'。
在故事板文件中查找 ...: Outlet = "play"
.
单击该条目并通过单击 play
旁边的 'x' 按钮将其删除。
这应该可以解决崩溃问题。
每当您看到以下形式的错误时:
... this class is not key value coding-compliant for the key ...
你应该检查你的故事板或 xib 中是否缺少插座。