我希望将自定义属性包装器应用于已经包装的变量,像(A)或(B)@Published
一样嵌套它们
(注意包装器的应用顺序)。
@Custom @Published var myVar
@Published @Custom var myVar
在(A)的情况下,我得到了错误
'wrappedValue' is unavailable: @Published is only available on properties of classes
对于(B)
error: key path value type 'Int' cannot be converted to contextual type 'Updating<Int>'
两者都不是特别有用。任何想法如何使它工作?
最小代码示例
import Combine
class A {
@Updating @Published var b: Int
init(b: Int) {
self.b = b
}
}
@propertyWrapper struct Updating<T> {
var wrappedValue: T {
didSet {
print("Update: \(wrappedValue)")
}
}
}
let a = A(b: 1)
let cancellable = a.$b.sink {
print("Published: \($0)")
}
a.b = 2
// Expected output:
// ==> Published: 1
// ==> Published: 2
// ==> Update: 2