带有 Xcode 11 beta 7 的 SwiftUI 不更新 List/ForEach 的内容 [英] SwiftUI with Xcode 11 beta 7 not updating contents of List / ForEach

查看:57
本文介绍了带有 Xcode 11 beta 7 的 SwiftUI 不更新 List/ForEach 的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试一个简单的功能来向列表添加新条目.视图只会添加一个新生成的.项目(无需用户输入).

I've been trying a simple feature to add new entries to a List. The View will just add a new generated. item (no need for user input).

struct PeopleList: View {
@ObservedObject var people: PersonStore
var body: some View {
    NavigationView {
        VStack {
            Section {
                Button(action: add) {
                    Text("Add")
                }
            }
            Section {
                List {
                    ForEach(people.people) { person in

                        NavigationLink(destination: PersonDetail(person: person)) {
                            PersonRow(person: person)
                        }
                    }
                }
            }
        }
    }
    .navigationBarTitle(Text("People"))
    .listStyle(GroupedListStyle())
}

func add() {
    let newID = (people.people.last?.id ?? 0) + 1

    self.people.people.append(Person(id: newID, name: ""))
}
}

这曾经在以前的测试版中有效,但由于某种原因它不再有效.当我点击添加时,应用会调用 add() 函数并将新条目添加到数组中,但视图根本没有更新.

This used to work in previous betas, but for some reason it's not working anymore. When I click on Add, the App does call the add() function and adds the new entry to the array, but the View is not updated at all.

这些是支持类:

    class PersonStore: ObservableObject {

    var people: [Person] {
        willSet {
            willChange.send()
        }
    }

    init(people: [Person] = []) {
        self.people = people
    }

    var willChange = PassthroughSubject<Void, Never>()
}

class Person: ObservableObject, Identifiable {

    var id: Int = 0 {
        willSet {
            willChange.send()
        }
    }

    var name: String {
        willSet {
            willChange.send()
        }
    }

     init(id: Int, name: String) {
        self.id = id
        self.name = name
    }

    var willChange = PassthroughSubject<Void, Never>()
}

#if DEBUG

let data = [
    Person(id: 1, name: "David"),
    Person(id: 2, name: "Anne"),
    Person(id: 3, name: "Carl"),
    Person(id: 4, name: "Amy"),
    Person(id: 5, name: "Daisy"),
    Person(id: 6, name: "Mike"),
]

#endif

以及支持意见:

struct PersonRow: View {

    @ObservedObject var person: Person

    var body: some View {
        HStack {
            Image(systemName: "\(person.id).circle")
            Text(person.name)
        }.font(.title)
    }
}

struct PersonDetail: View {

    @ObservedObject var person: Person

    var body: some View {
        HStack {

            Text("This is \(person.name)")
        }.font(.title)
    }
}

我发现有人遇到的问题与此处有点相关:SwiftUI:如果@,查看内容不会重新加载ObservedObject 是 UIViewController 的子类.这是错误还是我遗漏了什么?和这里:SwiftUI @Binding 不刷新视图

I've found someone with a problem that looks a bit related here: SwiftUI: View content not reloading if @ObservedObject is a subclass of UIViewController. Is this a bug or am I missing something? and here: SwiftUI @Binding doesn't refresh View

推荐答案

问题在于,当您实现自己的 ObservableObject 时,您使用了错误的发布者.ObservableObject 协议创建了 objectWillChange 发布者,但你永远不会使用它,因此 SwiftUI 永远不会被告知任何事情发生了变化.

The problem is that when you implemented your own ObservableObject you used the wrong publisher. The ObservableObject protocol creates the objectWillChange publisher but you never use it so that SwiftUI is never informed that anything has changed.


class Person: ObservableObject, Identifiable {
    let id: Int

    @Published
    var name: String

    init(id: Int, name: String) {
        self.id = id
        self.name = name
    }
}

我还没有通过编译器运行它,所以可能会有一些拼写错误.

I haven't run that through the compiler so there might be some typos.

您不必使用@Published,但对于像您这样的简单案例,它更容易.当然,您也需要更新其他课程.

You don't have to use @Published but for a simple case like yours it's easier. Of course you need to update your other class as well.

还有一件小事,id 要求永不改变,List 等人.使用它将您的数据与其创建的视图连接起来.

Another small thing, id is required to never change, List et al. use it to connect your data with the view they create.

这篇关于带有 Xcode 11 beta 7 的 SwiftUI 不更新 List/ForEach 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆