Skip to content

Instantly share code, notes, and snippets.

@joemasilotti
Last active August 4, 2020 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemasilotti/20fe721c071bc486f874098a33c66938 to your computer and use it in GitHub Desktop.
Save joemasilotti/20fe721c071bc486f874098a33c66938 to your computer and use it in GitHub Desktop.
SwiftUI List/ForEach with bindings
import SwiftUI
struct ContentView: View {
@ObservedObject private var signalStore = SignalStore()
var body: some View {
VStack {
List(signalStore.all) { signal in
SignalView(signal: self.signalStore.binded(signal: signal))
}
}
}
}
import Foundation
struct Signal: Identifiable {
let id = UUID()
}
extension Signal: Equatable {
static func ==(lhs: Signal, rhs: Signal) -> Bool {
return lhs.id == rhs.id
}
}
import SwiftUI
class SignalStore: ObservableObject {
var all = [Signal]()
func binded(signal: Signal) -> Binding<Signal> {
Binding(
get: { self.all[self.all.firstIndex(of: signal)!] },
set: { self.all[self.all.firstIndex(of: signal)!] = $0 }
)
}
}
import SwiftUI
struct SignalView: View {
@Binding var signal: Signal
var body: some View {
Text("...")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment