Avatar
@swift-5.2.5 @swift-5.3.3 class Record { var storage: [String: Any] init(_ storage: [String: Any]) { self.storage = storage } } extension Record { @propertyWrapper public struct Field<Value> { @available(*, unavailable) public var wrappedValue: Value { fatalError("only works on instance properties of classes") } private let key: String public init(key: String) { self.key = key } public static subscript<Instance: Record>( _enclosingInstance instance: Instance, wrapped wrappedKeyPath: KeyPath<Instance, Value>, storage storageKeyPath: ReferenceWritableKeyPath<Instance, Self> ) -> Value { let field = instance[keyPath: storageKeyPath] return instance.storage[field.key] as! Value } } } class User: Record { @Field(key: "id") var id: Int } let user = User(["id": 100]) print(user.id) (edited)