在我的Swift 2.3中,我有一个可以单独运行的计时器。我可以通过创建它的实例来访问计时器。 Swift 2.3中的代码:
Problem1 testProblem = new Problem1();
testProblem.PrintSolution();
Console.Read();
我可以输入以下内容启动计时器:
public struct Dispatch {
public typealias Block = () -> ()
}
extension Dispatch {
public static func timerAsync(queue: dispatch_queue_t = dispatch_get_global_queue(qos_class_main(), 0), interval: Double, block: Block) -> dispatch_source_t {
let timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, UInt64(interval * Double(NSEC_PER_SEC)), 100);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);
return timer;
}
}
extension dispatch_source_t {
public func invalidate() -> Self {
dispatch_source_cancel(self);
return self;
}
}
并输入以下命令停止计时器:
timer1 = timerAsync(interval: intervalTime){
//some code to run every intervalTime
}
但我无法弄清楚如何在Swift 3.0中编写等效代码。任何帮助表示赞赏。
谢谢!