Swift NSTimer didn't work

隐身守侯 提交于 2019-12-23 04:58:04

问题


I make a "Command Line Tool", and I need use NSTimer. I start the timer, but it doesn't work...

import Foundation

class ct : NSObject {
    func start() {
        var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
    }

    func update() {
        println("test timer");
    }
}
var a = ct();
a.start()

while(true) { sleep(10000000) }

回答1:


NSTimer needs a run loop to work properly, a CLI doesn't have/need one by default.

Call

CFRunLoopRun()

to start the run loop and

CFRunLoopStop(CFRunLoopGetCurrent())

to stop it and don't forget to return appropriate return values.



来源:https://stackoverflow.com/questions/32140470/swift-nstimer-didnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!