从昨天开始我就被这个问题困住了,我找不到任何解决方案。
import UIKit
class ViewController: UIViewController {
@IBAction func cameraButton(_ sender: Any) {
cameraResultDisplay.text = "Camera Button Pressed!"
}
@IBOutlet weak var cameraResultDisplay: UILabel!
func processTimer() {
print("A second has passed!")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var timer = Timer()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: Selector("processTimer"), userInfo: nil, repeats: true)
}
}
无论我做什么,它仍然会抛出这个错误,并且这个屏幕莫名其妙地显示出来:
http://prntscr.com/n1wmcl
这是完整的错误日志:
2019-03-23 21:42:52.788044+0530 Menu Bars[37292:1510342] -[Menu_Bars.ViewController processTimer]: unrecognized selector sent to instance 0x7fa185607fe0
2019-03-23 21:42:52.790421+0530 Menu Bars[37292:1510342] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Menu_Bars.ViewController processTimer]: unrecognized selector sent to instance 0x7fa185607fe0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ff2f1bb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x000000010f4cd735 objc_exception_throw + 48
2 CoreFoundation 0x000000010ff4df44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 UIKitCore 0x00000001138a9b4a -[UIResponder doesNotRecognizeSelector:] + 287
4 CoreFoundation 0x000000010ff33ed6 ___forwarding___ + 1446
5 CoreFoundation 0x000000010ff35da8 _CF_forwarding_prep_0 + 120
6 Foundation 0x000000010ef70881 __NSFireTimer + 83
7 CoreFoundation 0x000000010fe94f34 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
8 CoreFoundation 0x000000010fe94b32 __CFRunLoopDoTimer + 1026
9 CoreFoundation 0x000000010fe9439a __CFRunLoopDoTimers + 266
10 CoreFoundation 0x000000010fe8ea1c __CFRunLoopRun + 2252
11 CoreFoundation 0x000000010fe8de11 CFRunLoopRunSpecific + 625
12 GraphicsServices 0x000000011900c1dd GSEventRunModal + 62
13 UIKitCore 0x000000011387b81d UIApplicationMain + 140
14 Menu Bars 0x000000010eba59d7 main + 71
15 libdyld.dylib 0x0000000112388575 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我正在学习 Udemy 的在线类(class),我编写的代码与类(class)中的人编写的代码完全相同。我尝试了一些事情,但我认为我做了一些愚蠢的事情,这就是它不起作用的原因。现在我从 2 天起就被这个问题困扰了,这让我很头疼,没有人能帮我解决这个问题。
请帮我解决这个问题。
最佳答案
您需要将 @objc
添加到函数的定义中,以便将其与选择器一起使用:
@objc func processTimer() {
print("A second has passed!")
}
但不推荐以这种方式使用选择器,而是执行以下操作:
用以下代码替换您安排计时器的代码行:
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(processTimer), userInfo: nil, repeats: true)
当您将函数作为选择器参数传递时,您需要使用以下语法:
selector: #selector(functionName)
关于ios - 在 swift 4.2 中发送到实例的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55315791/