Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import SwiftUI
import Combine
class MyDatabase: BindableObject {
let didChange = PassthroughSubject<MyDatabase, Never>()
var contacts: [Contact] = [
Contact(id: 1, name: "Anna"), Contact(id: 2, name: "Beto"),
Contact(id: 3, name: "Jack"), Contact(id: 4, name: "Sam")
] {
didSet {
didChange.send(self)
}
}
struct Contact {
var id: Int
var name: String
}
}
struct ContactsList: View {
@EnvironmentObject private var database: MyDatabase
var body: some View {
NavigationView {
List($database.contacts.identified(by: \.value.id)) { contact in
NavigationButton(destination: ContactDetail(contact: contact)) {
Text(verbatim: contact.value.name)
}
}
.navigationBarTitle(Text("Contacts"))
}
}
}
struct ContactDetail: View {
@Binding var contact: MyDatabase.Contact
var body: some View {
VStack {
TextField($contact[\.name])
.textFieldStyle(.roundedBorder)
.font(.title)
.padding()
Spacer()
}
.navigationBarTitle(Text("Edit"), displayMode: .inline)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.