简体   繁体   中英

Swift NSTimer didn't work

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) }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM