在前文的基础上,还缺少timer的逻辑,在SwiftUI中可以通过接收publisher发出的事件。我们使用timer作为publisher,向视图发出事件,视图收到事件就可以做出响应
添加一个publisher的class类型
class TimePublisher {
let currentTimePublisher = Timer.TimerPublisher(interval: 1, runLoop: .main, mode: .default)
let cancelable: AnyCancellable?
init() {
self.cancelable = currentTimePublisher.