Using an NSTimer in Swift

前端 未结 10 1518
生来不讨喜
生来不讨喜 2020-12-05 06:19

In this scenario, timerFunc() is never called. What am I missing?

class AppDelegate: NSObject, NSApplicationDelegate {

    var myTimer: NSTimer? = nil

             


        
10条回答
  •  隐瞒了意图╮
    2020-12-05 06:47

    i use a similar approach to Luke. Only a caveat for people who are "private methods" purists:

    DO NOT make callback private in Swift.

    If You write:

    private func timerCallBack(timer: NSTimer){
    

    ..

    you will get:

    timerCallBack:]: unrecognized selector sent to instance... Terminating app due to uncaught exception 'NSInvalidArgumentException'

提交回复
热议问题