I'm trying to listen to @State
changes and followed this answer. Here's my code:
import SwiftUI
struct ContentView: View {
@State var isOn = false
var body: some View {
Toggle("Selection", isOn: $isOn)
.onReceive(Just(isOn)) { isOn in
print("Toggle is on? \(isOn)")
}
}
}
It doesn't compile: I get "Cannot find 'Just' in scope"
Just
is part of the Combine framework. But, I thought SwiftUI already imported Combine? I Command-clicked import SwiftUI
, then pressed Jump to Definition
, and there it was at the top.
Once I add import Combine
to my code, it compiles. But shouldn't this be redundant and unnecessary?