In this scenario, timerFunc() is never called. What am I missing?
class AppDelegate: NSObject, NSApplicationDelegate {
var myTimer: NSTimer? = nil
func timerFunc() {
println("timerFunc()")
}
func applicationDidFinishLaunching(aNotification: NSNotification?) {
myTimer = NSTimer(timeInterval: 5.0, target: self, selector:"timerFunc", userInfo: nil, repeats: true)
}
}
Swift 3.0 syntax for the run loop thingy:
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:
..
you will get:
timerCallBack:]: unrecognized selector sent to instance... Terminating app due to uncaught exception 'NSInvalidArgumentException'
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:
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
For Swift 3