Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SwiftUI中更新树结构的UI_Swift_Tree_Swiftui_Children_Observedobject - Fatal编程技术网

在SwiftUI中更新树结构的UI

在SwiftUI中更新树结构的UI,swift,tree,swiftui,children,observedobject,Swift,Tree,Swiftui,Children,Observedobject,我有一个ObserveObject类,Model0,它有一个已发布的对象嵌套的。嵌套的对象表示递归树结构。我希望能够更新树结构并适当地更新UI。但是,在下面的代码中,当我更新nested对象的子对象时,UI没有更新,我假设是因为对nested的引用没有更改。当我更改树结构中的任何嵌套对象时,如何使UI更新 class Nested: Identifiable { var id: UUID = UUID() var name: String var children:[Ne

我有一个ObserveObject类,
Model0
,它有一个
已发布的
对象
嵌套的
。嵌套的
对象表示递归树结构。我希望能够更新树结构并适当地更新UI。但是,在下面的代码中,当我更新
nested
对象的子对象时,UI没有更新,我假设是因为对
nested
的引用没有更改。当我更改树结构中的任何
嵌套对象时,如何使UI更新

class Nested: Identifiable {
    var id: UUID = UUID()
    var name: String
    var children:[Nested]
    
    init(name: String, children:[Nested]) {
        self.children = children
        self.name = name
    }
}

class Model0 : ObservableObject {
    @Published var nested: Nested
    
    init(nested: Nested) {
        self.nested = nested
    }
    
    func update() {
        nested.children[0] = Nested(name:"New", children: [])
    }
}

struct NestedView: View {
    var nested: Nested
    
    var body: some View {
        VStack {
            Text(nested.name)
            
            ForEach(nested.children) { c in
                NestedView(nested: c)
            }
        }
    }
}


struct Test: View {
    @ObservedObject var model: Model0 = Model0(nested: Nested(name: "Parent" ,children: [Nested(name: "Child", children: [])]))
    
    var body: some View {
        VStack {
            Button(action: {
                self.model.update()
            }) {
             Text("Update")
            }
            NestedView(nested: model.nested)
        }
    }
}

若您决定建立引用类型模型,那个么您需要使它们都是可观察的,这样每个级别都可以更新相应的视图。当然,每个级别模型都有单独的视图

考虑到以上因素,这里有一个解决方案。使用Xcode 12/iOS 14进行测试

仅限修改部分:

class Nested: ObservableObject, Identifiable {
    var id: UUID = UUID()
    @Published var name: String
    @Published var children:[Nested]

    init(name: String, children:[Nested]) {
        self.children = children
        self.name = name
    }
}

struct NestedView: View {
    @ObservedObject var nested: Nested

    var body: some View {
        VStack {
            Text(nested.name)

            ForEach(nested.children) { c in
                NestedView(nested: c)
            }
        }
    }
}