我已经使用DispatchSourceTimer制作了一个计时器,如下所示:
class Timer: : NSObject, ObservableObject {
@Published var counterTime: Int = 0
var timer: DispatchSourceTimer?
func startTimer() {
let queue = DispatchQueue(label: "com.app.watchkitapp", qos: .userInteractive)
timer = DispatchSource.makeTimerSource(flags: .strict, queue: queue) <--- Xcode point out the error at this line
timer?.schedule(deadline: .now(), repeating: 1)
timer?.setEventHandler {
DispatchQueue.main.async {
self.counterTime += 1
}
}
timer?.resume()
}
func stopTimer() {
timer?.suspend()
}
func resetTimer() {
timer?.cancel()
self.counterTime = 0
}
}
但每次我try 执行"stopTimer()"函数(以停止计时器)时,我都会遇到这个错误:
Thread 1: EXC_BREAKPOINT (code=1, subcode=0x102f34210)
顺便说一句,我想做一个计时器,可以开始,暂停和重置在watchos.谢谢大家!