Using an NSTimer in Swift

前端 未结 10 1521
生来不讨喜
生来不讨喜 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 07:01

    This is a bit of code, that demonstrates how to call a function (delayed) with AND without a parameter.

    Use this in a new project in xCode (singleViewApplication) and put the code into the standard viewController:

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
    
            NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("delayedFunctionWithoutParameter:"), userInfo: nil, repeats: false)
    
            let myParameter = "ParameterStringOrAnyOtherObject"
    
            NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: Selector("delayedFunctionWithParameter:"), userInfo: myParameter, repeats: false)
        }
    
        // SIMPLE TIMER - Delayed Function Call
        func delayedFunctionWithoutParameter(timer : NSTimer) {
            print("This is a simple function beeing called without a parameter passed")
            timer.invalidate()
        }
    
        // ADVANCED TIMER - Delayed Function Call with a Parameter
        func delayedFunctionWithParameter(timer : NSTimer) {
    
            // check, wether a valid Object did come over
            if let myUserInfo: AnyObject = timer.userInfo {
                // alternatively, assuming it is a String for sure coming over
                // if let myUserInfo: String = timer.userInfo as? String {
                // assuming it is a string comming over
                print("This is an advanced function beeing called with a parameter (in this case: \(myUserInfo)) passed")
            }
    
            timer.invalidate()
        }
    }
    

    Notice, that in any case you should implement the delayed function with the parameter (timer : NSTimer) to be able to invalidate (terminate, end) the timer. And with the passend "timer" you have also access to the userInfo (and there you can put any Object, not only String-Objects, as well collection types such as arrays and dictionaries).

    Original Apples documentations says "" -> The timer passes itself as the argument, thus the method would adopt the following pattern: - (void)timerFireMethod:(NSTimer *)timer Read fully -> here

提交回复
热议问题