为什么我的 SwiftUI 应用程序中没有更新 ObservedObject 数组? [英] Why an ObservedObject array is not updated in my SwiftUI application?

查看:28
本文介绍了为什么我的 SwiftUI 应用程序中没有更新 ObservedObject 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm playing with SwitUI, trying to understand how ObservableObject works. I have an array of Person objects. When I add a new Person into the array, it is reloaded in my View. However, if I change the value of an existing Person, it is not reloaded in the 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 的名字会正确显示.但是,如果我只是更改名称,则不会在视图中显示.

If I uncomment the line to add a new Person (John), the name of Jaime is shown properly. However if I just change the name, this is not shown in the View.

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

I'm afraid I'm doing something wrong or maybe I don't get how the ObservedObjects work with Arrays.

欢迎任何帮助或解释!

推荐答案

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

You can use a struct instead of a class. Because of a struct's value semantics, a change to a person's name is seen as a change to Person struct itself, and this change is also a change to the people array so @Published will send the notification and the View body will be recomputed.

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 数组保持不变,因此主体不会发出任何内容.但是,您可以手动调用它,让它知道:

Alternatively (and not recommended), Person is a class, so it is a reference type. When it changes, the People array remains unchanged and so nothing is emitted by the subject. However, you can manually call it, to let it know:

Button(action: {
    self.mypeople.objectWillChange.send()
    self.mypeople.people[0].name="Jaime"    
}) {
    Text("Add/Change name")
}

这篇关于为什么我的 SwiftUI 应用程序中没有更新 ObservedObject 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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