swiftui - 为什么我的 SwiftUI 应用程序中没有更新 ObservedObject 数组?

标签 swiftui

我正在使用 SwitUI,试图了解 ObservableObject 的工作原理。我有一个 Person 对象数组。当我将一个新的 Person 添加到数组中时,它会重新加载到我的 View 中。但是,如果我更改现有人员的值,则不会在 View 中重新加载

//  NamesClass.swift
import Foundation
import SwiftUI
import Combine

class Person: ObservableObject,Identifiable{
    var id: Int
    @Published var name: String

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

}

class People: ObservableObject{
    @Published var people: [Person]

    init(){
        self.people = [
            Person(id: 1, name:"Javier"),
            Person(id: 2, name:"Juan"),
            Person(id: 3, name:"Pedro"),
            Person(id: 4, name:"Luis")]
    }

}
struct ContentView: View {
    @ObservedObject var mypeople: People

    var body: some View {
        VStack{
            ForEach(mypeople.people){ person in
                Text("\(person.name)")
            }
            Button(action: {
                self.mypeople.people[0].name="Jaime"
                //self.mypeople.people.append(Person(id: 5, name: "John"))
            }) {
                Text("Add/Change name")
            }
        }
    }
}

如果我取消注释该行以添加一个新人 (John),Jaime 的名字会正确显示。但是,如果我只是更改名称,则不会在 View 中显示。

恐怕我做错了什么,或者我不明白 ObservedObjects 如何与数组一起工作。

欢迎任何帮助或解释!

最佳答案

您可以使用结构而不是类。由于结构体的值语义,对人名的更改被视为对 Person 结构体本身的更改,并且此更改也是对 people 数组的更改,因此 @Published 将发送通知并重新计算 View 主体。

import Foundation
import SwiftUI
import Combine

struct Person: Identifiable{
    var id: Int
    var name: String

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

}

class Model: ObservableObject{
    @Published var people: [Person]

    init(){
        self.people = [
            Person(id: 1, name:"Javier"),
            Person(id: 2, name:"Juan"),
            Person(id: 3, name:"Pedro"),
            Person(id: 4, name:"Luis")]
    }

}

struct ContentView: View {
    @StateObject var model = Model()

    var body: some View {
        VStack{
            ForEach(model.people){ person in
                Text("\(person.name)")
            }
            Button(action: {
                self.mypeople.people[0].name="Jaime"
            }) {
                Text("Add/Change name")
            }
        }
    }
}
或者(不推荐),Person是一个类,所以它是一个引用类型。当它改变时,People数组保持不变,因此主体不会发出任何内容。但是,您可以手动调用它,让它知道:
Button(action: {
    self.mypeople.objectWillChange.send()
    self.mypeople.people[0].name="Jaime"    
}) {
    Text("Add/Change name")
}

关于swiftui - 为什么我的 SwiftUI 应用程序中没有更新 ObservedObject 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57459727/

相关文章:

图像未出现在 SwiftUI 中

ios - 如何在 SwiftUI 中使用 Toast-Swift?

core-data - 使用公共(public) CloudKit 数据库 (NSPersistentCloudkitContainer) - macOS

macos - 如何从 SwiftUI View 中运行 AppleScript?

ios - SwiftUI 无法绑定(bind)注入(inject)的值

swift - 在 Xcode11 beta 4 中工作但在 beta 5 中停止工作的代码有问题

ios - 如何在 SwiftUI 中制作水平列表?

swift - 使用 MVVM-Pattern 处理 SwiftUI 和 CoreLocation

swift - 如何捕获通过 SwiftUI 中的函数更改的值更改?

ios - 在 SwiftUI 中使用协调器模式时未调用 CNContactViewControllerDelegate