I wish to apply a custom property wrapper to a variable already wrapped in @Published
, nesting them like
(A) @Custom @Published var myVar
or
(B) @Published @Custom var myVar
(notice the application order of the wrappers).
In the case of (A) I get the error
'wrappedValue' is unavailable: @Published is only available on properties of classes
and for (B)
error: key path value type 'Int' cannot be converted to contextual type 'Updating<Int>'
neither of which are particularly helpful. Any ideas how to make it work?
Minimal code example
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