迅速将无法识别的选择器发送到实例4.2

时间:2019-03-23 16:17:36

标签: ios swift xcode

从昨天开始,我就一直困扰着这个问题,我找不到任何解决方案。

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的在线课程,并且编写的代码与该课程的编写者完全相同。我尝试了一些操作,但是我认为我做了一些愚蠢的操作,这就是为什么它不起作用的原因。现在我从两天开始就一直困扰着这个问题,这让我头疼,没有人可以帮助我解决这个问题。

请多谢我。

1 个答案:

答案 0 :(得分:0)

您需要在函数的定义中添加@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)