从Swift的属性包装器中,您可以有人引用拥有被包装属性的类实例或被删除的实例吗?使用self显然不起作用,也不起作用super。
我试图传递self给属性包装器,init()但这也不起作用,因为self在评估Configuration时on 尚未定义@propertywrapper。
我的用例在用于管理大量设置或配置的类中。如果有任何属性更改,我只想通知感兴趣的人某些更改。他们实际上并不需要只知道哪个值,因此实际上不必为每个属性使用KVO或Publisher。
属性包装器看起来很理想,但是我不知道如何传递对包装器可以回调的拥有实例的某种引用。
参考文献:
enum PropertyIdentifier {
case backgroundColor
case textColor
}
@propertyWrapper
struct Recorded<T> {
let identifier:PropertyIdentifier
var _value: T
init(_ identifier:PropertyIdentifier, defaultValue: T) {
self.identifier = identifier
self._value = defaultValue
}
var value: T {
get { _value }
set {
_value = newValue
// How to callback to Configuration.propertyWasSet()?
//
// [self/super/...].propertyWasSet(identifier)
}
} …Run Code Online (Sandbox Code Playgroud)