Michael Long
Feb 21, 2021

--

Or start with the delay....

```

final class ColorService: ObservableObject {

@Published var color: Color = .red

private var cancellable: AnyCancellable?

func start() {

cancellable = Timer.publish(every: 2.0, on: .main, in: .default)

.autoconnect()

.scan(0) { (i, _) -> Int in i + 1 }

.map { [Color.red, Color.blue, Color.green, Color.orange][$0 % 4] }

.sink { [weak self] (color) in

self?.color = color

}

}

}

```

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

No responses yet