我在控制台中遇到此错误,我不知道如何修复它或它是什么。
2015-06-30 21:09:28.268 stop watch[1394:373417] -[stop_watch.ViewController
result]: unrecognized selector sent to instance 0x7fa7f0513df0
2015-06-30 21:09:28.270 stop watch[1394:373417] *** Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason: '-
[stop_watch.ViewController result]: unrecognized selector sent to instance
0x7fa7f0513df0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010bee7a75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010da3fbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010beeed1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010be469dc ___forwarding___ + 988
4 CoreFoundation 0x000000010be46578 _CF_forwarding_prep_0 + 120
5 Foundation 0x000000010c31f844 __NSFireTimer + 83
6 CoreFoundation 0x000000010be4f6c4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
7 CoreFoundation 0x000000010be4f285 __CFRunLoopDoTimer + 1045
8 CoreFoundation 0x000000010be1259d __CFRunLoopRun + 1901
9 CoreFoundation 0x000000010be11bc6 CFRunLoopRunSpecific + 470
10 GraphicsServices 0x0000000110003a58 GSEventRunModal + 161
11 UIKit 0x000000010c77a580 UIApplicationMain + 1282
12 stop watch 0x000000010bd067be top_level_code + 78
13 stop watch 0x000000010bd067fa main + 42
14 libdyld.dylib 0x000000010e21b145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
抱歉,我希望自己能更清楚,但我真的不知道问题所在。
此外,我不知道这是否会有所帮助,但我会将代码放在下面。
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var count = 0
var condition = 1
@IBOutlet weak var main: UILabel!
@IBOutlet weak var stopOut: UIButton!
@IBAction func start(sender: AnyObject) {
func result() {
count++
}
condition = 1
main.text = String(count)
}
@IBAction func stop(sender: AnyObject) {
if condition == 1 {
timer.invalidate()
condition = 0
stopOut.setTitle("stop", forState: UIControlState.Normal)
}
else {
stopOut.setTitle("clear", forState: UIControlState.Normal)
timer.invalidate()
count = 0
main.text = String(count)
condition = 1
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
所以我做了一些最近的更改,并且我将结果函数从启动函数中取出,现在它启动了,但现在当我按下启动时,我需要连续按下启动按钮进行更新。那么我该如何才能使它只在按下按钮时开始滴答,并且每秒继续更新为下一个数字。
答案 0 :(得分:0)
在ViewController
课程中,您的viedDidLoad()
方法应更改为以下内容(删除Selector
,有关详细信息,请参阅this Stack Overflow answer):
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "result", userInfo: nil, repeats: true)
}
然后,将result()
的声明移出@IBAction
名为start
的声明。也就是说,在ViewController
内部声明它如此:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
// ...
}
// ... other functions here
func result() {
self.count++
}
}
以后,每当您看到表单错误时
[someObject someFunction]: unrecognized selector sent to instance ...
首先应检查以确保someObject
声明一个名为someFunction
的函数。这几乎总是问题。
为了响应您的修改,您应该像这样实施result()
:
func result() {
self.count++
main.text = "\(self.count)"
}
这将导致每次定时器触发时UILabel
中的文本都会更新。