swift @propertyWrapper的使用

2022-02-28  本文已影响0人  迷路的小小
@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 ------
上一篇 下一篇

猜你喜欢

热点阅读