Using an NSTimer in Swift -
in scenario, timerfunc() never called. 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) } }
you can create scheduled timer automatically adds runloop , starts firing:
nstimer.scheduledtimerwithtimeinterval(0.5, target: self, selector: "timerdidfire:", userinfo: userinfo, repeats: true)
or, can maintain current code, , add together timer runloop when you're ready it:
let mytimer = nstimer(timeinterval: 0.5, target: self, selector: "timerdidfire:", userinfo: nil, repeats: true) nsrunloop.currentrunloop().addtimer(mytimer, formode: nsrunloopcommonmodes)
swift nstimer
No comments:
Post a Comment