Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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