Daniel Lyons' Notes

@dynamicMemberLookup in Swift

Using KeyPaths

@dynamicMemberLookup
class Reference<Value> {
    fileprivate(set) var value: Value

    init(value: Value) {
        self.value = value
    }

    subscript<T>(dynamicMember keyPath: KeyPath<Value, T>) -> T {
        value[keyPath: keyPath]
    }
}

class MutableReference<Value>: Reference<Value> {
    subscript<T>(dynamicMember keyPath: WritableKeyPath<Value, T>) -> T {
        get { value[keyPath: keyPath] }
        set { value[keyPath: keyPath] = newValue }
    }
}

Using Strings

@dynamicMemberLookup
struct Settings {
    var colorTheme = ColorTheme.modern
    var itemPageSize = 25
    var keepUserLoggedIn = true

    subscript(dynamicMember member: String) -> Any? {
        switch member {
        case "colorTheme":
            return colorTheme
        case "itemPageSize":
            return itemPageSize
        case "keepUserLoggedIn":
            return keepUserLoggedIn
        default:
            return nil
        }
    }
}

Sources

@dynamicMemberLookup in Swift
Interactive graph
On this page
Using KeyPaths
Using Strings
Sources