У меня есть модель данных с вложенными отношениями с родителями/ребенком/внуком:
Код: Выделить всё
struct Parent: Identifiable, Hashable {
let id: UUID
var children = [Child]()
}
struct Child: Identifiable, Hashable {
let id: UUID
var children = [Grandchild]()
}
struct Grandchild: Identifiable, Hashable {
let id: UUID
var name = ""
}
Код: Выделить всё
struct ParentSettingsView: View {
@Binding var parent: Parent
var body: some View {
List(parent.children.indices, id: \.self) { i in
NavigationLink(parent.children[i].id.uuidString, value: parent.children[i])
}
.navigationDestination(for: Child.self) { child in
let idx = parent.children.firstIndex(where: {$0.id == child.id})!
ChildSettingsView(child: $parent.children[idx])
}
}
}
// ChildSettingsView is basically identical
Код: Выделить всё
struct GrandchildSettingsView: View {
@Binding var grandchild: Grandchild
var body: some View {
TextField("Name", text: $grandchild.name)
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... alue-types
Мобильная версия