源码

首页 » 归档 » 源码 » @Published属性包装器不能处理ObservableObject的子类

@Published属性包装器不能处理ObservableObject的子类

我有一个符合@ObservableObject协议的类/uff0c并使用它自己的变量和@Published属性包装器来创建一个子类来管理状态。
在使用子类时/uff0c似乎忽略了@published属性包装器。 有谁知道这是否是预期的行为/uff0c是否有解决方法/uff1f
我正在运行iOS 13 Beta 8和xCode Beta 6。
这是我所看到的一个例子。 在MyTestObject上更新TextField时/uff0c使用aString值正确更新Text视图。 如果我更新MyInheritedObjectTextField/uff0c则不会在/u201c文本/u201d视图中更新anotherString值。

import SwiftUI

class MyTestObject: ObservableObject {
    @Published var aString: String = ""

}

class MyInheritedObject: MyTestObject {
    @Published var anotherString: String = ""
}

struct TestObserverWithSheet: View {
    @ObservedObject var myTestObject = MyInheritedObject()
    @ObservedObject var myInheritedObject = MyInheritedObject()

    var body: some View {
        NavigationView {
            VStack(alignment: .leading) {
                TextField("Update aString", text: self.$myTestObject.aString)
                Text("Value of aString is: /(self.myTestObject.aString)")

                TextField("Update anotherString", text: self.$myInheritedObject.anotherString)
                Text("Value of anotherString is: /(self.myInheritedObject.anotherString)")
            }
        }
    }
}

最后找到了解决这个问题的方法。如果从子类中移除属性包装器/uff0c并对变量调用baseClass objectWillChange.send/uff08/uff09/uff0c则状态将正确更新。
注意/uff1a不要在子类上重新声明let objectwillchange=passthroughSubject/uff08/uff09/uff0c因为这将再次导致状态不能正确更新。
我希望这是在将来的版本中修复的/uff0c因为objectwillchange.send/uff08/uff09需要维护很多样板文件。
下面是一个完全有效的例子/uff1a

   import SwiftUI

    class MyTestObject: ObservableObject {
        @Published var aString: String = ""

    }

    class MyInheritedObject: MyTestObject {
        // Using @Published doesn't work on a subclass
        // @Published var anotherString: String = ""

        // If you add the following to the subclass updating the state also doesn't work properly
        // let objectWillChange = PassthroughSubject()

        // But if you update the value you want to maintain state 
        // of using the objectWillChange.send() method provided by the 
        // baseclass the state gets updated properly... Jaayy!
        var anotherString: String = "" {
            willSet { self.objectWillChange.send() }
        }
    }

    struct MyTestView: View {
        @ObservedObject var myTestObject = MyTestObject()
        @ObservedObject var myInheritedObject = MyInheritedObject()

        var body: some View {
            NavigationView {
                VStack(alignment: .leading) {
                    TextField("Update aString", text: self.$myTestObject.aString)
                    Text("Value of aString is: /(self.myTestObject.aString)")

                    TextField("Update anotherString", text: self.$myInheritedObject.anotherString)
                    Text("Value of anotherString is: /(self.myInheritedObject.anotherString)")
                }
            }
        }
    }

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/publishedshuxingbaozhuangqibunengchuliobservableobjectdezilei.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:8 月 25, 2019 at 08:29 下午

热评文章