【发布时间】:2022-11-20 06:42:04
【问题描述】:
如何将可绑定对象传递到 ForEach 循环内的视图中?
下面的最小可重现代码。
class Person: Identifiable, ObservableObject {
let id: UUID = UUID()
@Published var healthy: Bool = true
}
class GroupOfPeople {
let people: [Person] = [Person(), Person(), Person()]
}
public struct GroupListView: View {
//MARK: Environment and StateObject properties
//MARK: State and Binding properties
//MARK: Other properties
let group: GroupOfPeople = GroupOfPeople()
//MARK: Body
public var body: some View {
ForEach(group.people) { person in
//ERROR: Cannot find '$person' in scope
PersonView(person: $person)
}
}
//MARK: Init
}
public struct PersonView: View {
//MARK: Environment and StateObject properties
//MARK: State and Binding properties
@Binding var person: Person
//MARK: Other properties
//MARK: Body
public var body: some View {
switch person.healthy {
case true:
Text("Healthy")
case false:
Text("Not Healthy")
}
}
//MARK: Init
init(person: Binding<Person>) {
self._person = person
}
}
我得到的错误是Cannot find '$person' in scope。我知道在执行 ForEach 循环时,变量的 @Binding 部分不在范围内。我正在寻找关于不同模式的建议,以完成 @Binding 对象到 SwiftUI 列表中的视图。
【问题讨论】:
-
您的示例中没有任何内容要求您将绑定传递给您的
PersonView,因此简单的答案就是删除@Binding并传递person。更复杂的答案可能是您需要考虑您的模型对象。您可能需要的不仅仅是一个简单的数组,但您还没有解释为什么思考你需要一个绑定
标签: ios arrays swift swiftui observableobject