Work on the playground with Swift 3.0 I have this code:
struct Test { func run() { var timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in print("pop") } } } let test = Test() test.run()
But nothing prints on the console. I read How can I use NSTimer in Swift? , and most of the use of the timer that I saw in answers and tutorials online includes a selector, so I tried this
class Test { func run() { var timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.peep), userInfo: nil, repeats: false) } @objc func peep() { print("peep") } } let test = Test() test.run()
Still nothing is printed on the console. If I add timer.fire() , I get console fingerprints, but obviously this defeats the target. What do I need to change to start the timer?
EDIT:
So adding CFRunLoopRun() after I called the run method for my Test structure did the trick. Many thanks to those who answered, especially @AkshayYaduvanshi (who commented on me pointed to CFRunLoopRun() ) and @JoshCaswell (whose answer was triggered by the fact that I only work with the start cycle).
source share