Spot the bug
The clock never updates. Tap the line that causes it.
final class ClockModel {var now = Date()func start() {_ = Timer.publish(every: 1, on: .main, in: .common).autoconnect().sink { [weak self] date in self?.now = date }}}
Hint 1 of 1
What happens when an AnyCancellable is deallocated?
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 4
_ = Timer.publish(every: 1, on: .main, in: .common)
Line 4 discards the AnyCancellable, so the subscription is cancelled at once. Store it: .store(in: &cancellables) with private var cancellables = Set<AnyCancellable>().