swiftstaticunrecognized-selector

Static function unrecognized selector sent to class


I have a simple ViewController with a timer that triggered every 3 seconds, when I use the following code, it works as expected.

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        var myTimer = MyTimer()
        myTimer.triggerTimer()
    }
}

class MyTimer: NSObject {
    var timer: Timer?

    func triggerTimer() {
        DispatchQueue.main.async {
            if self.timer == nil {
                self.timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.timeout), userInfo: nil, repeats: true)
            }
        }
    }

    @objc func timeout() {
        print("timeout")
    }
}

But when I change timer and triggerTimer() to static, the error occurs. This is the code that invoked the error:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        MyTimer.triggerTimer()
    }
}

class MyTimer: NSObject {
    static var timer: Timer?

    static func triggerTimer() {
        DispatchQueue.main.async {
            if self.timer == nil {
                self.timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.timeout), userInfo: nil, repeats: true)
            }
        }
    }

    @objc func timeout() {
        print("timeout")
    }
}

The error is:

unrecognized selector sent to class 0x10906b338'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a2dc1e6     __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x0000000109971031 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010a35d6c4 +[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010a25e898 ___forwarding___ + 1432
    4   CoreFoundation                      0x000000010a25e278 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001093db4dd __NSFireTimer + 83
    6   CoreFoundation                      0x000000010a26be64 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x000000010a26ba52 __CFRunLoopDoTimer + 1026
    8   CoreFoundation                      0x000000010a26b60a __CFRunLoopDoTimers + 266
    9   CoreFoundation                      0x000000010a262e4c __CFRunLoopRun + 2252
    10  CoreFoundation                      0x000000010a26230b CFRunLoopRunSpecific + 635
    11  GraphicsServices                    0x000000010fe57a73 GSEventRunModal + 62
    12  UIKit                               0x000000010a7590b7 UIApplicationMain + 159
    13  StaticProject                       0x0000000109067b27 main + 55
    14  libdyld.dylib                       0x000000010e747955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I have search through the website and see some similar questions, but I can't find the answer that is discuss about this. Can anyone please give me some hints? Thank you.


Solution

  • Since now the timer: Timer? and triggerTimer() are static, You need to make the timeout method as static too, make following changes to the code....

    @objc static func timeout() {
       print("timeout")
    }