Last active
August 4, 2020 19:27
-
-
Save joemasilotti/20fe721c071bc486f874098a33c66938 to your computer and use it in GitHub Desktop.
SwiftUI List/ForEach with bindings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Signal: Identifiable { | |
let id = UUID() | |
} | |
extension Signal: Equatable { | |
static func ==(lhs: Signal, rhs: Signal) -> Bool { | |
return lhs.id == rhs.id | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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