Constant bindings as placeholders
27 Jan 2021 ⇐ Notes archive(This is an entry in my technical notebook. There will likely be typos, mistakes, or wider logical leaps — the intent here is to “let others look over my shoulder while I figure things out.”)
When building layouts in SwiftUI, I’ll often sketch out the views — ignoring data flow — and then turn my attention towards state management. However, since many view initializers require bindings (e.g. TextField.init
or Toggle.init
), that puts things in an awkward spot.
What do I slot in if I haven’t determined whether to back the view with @State
, @ObservedObject
, another @Binding
, TCA-backed state, or the like?
Is there a placeholder I can use in the same way fatalError
appeases the compiler while I think through an implementation?1
Brandon and Stephen showed a technique early in episode #131 (timestamped).
For now, we’ll use a constant binding because we don’t have anywhere to send the user’s changes, but we will get to that soon.
Aha! That’s a solid trick — here’s a Toggle
example with its binding fixed to false
.
import SwiftUI
struct SampleView: View {
var body: some View {
Toggle("A toggle", isOn: .constant(false))
}
}
-
Never
as a universal “bottom” subtype was unfortunately not pursued in SE-0102. ↩