@propertyWrapper
struct Map {
private var value: String = "123"
var wrappedValue: String {
get {
return value
}
set {
value = newValue
}
}
var projectedValue: Int? {
Int(value)
}
static subscript<EnclosingSelf: AnyObject>(
_enclosingInstance object: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, String>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Map>
) -> String {
get {
object[keyPath: storageKeyPath].value
}
set {
object[keyPath: storageKeyPath].value = newValue
print(newValue, "------")
}
// TODO: Benchmark and explore a possibility to use _modify
}
}
var text = Text()
test.mothds = "20"
// 20 ------
-
projectedValue
启用$
语法糖
- 函数
static subscript<EnclosingSelf: AnyObject>( _enclosingInstance object: EnclosingSelf, wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, String>, storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Map> ) -> String
-
EnclosingSelf
可以不是AnyObject
,但如此只能使用get
属性,同时wrappedValue
也只能是可读属性。
- 标签
_enclosingInstance
、wrapped
、storage
不可改变,否则无效
网友评论