Refrence key string values using KeyPath

I built a pacakge to easily access the userDefault (GitHub - EngOmarElsayed/SwiftUserDefaults: This is very easy API to use to easily access and set userDefaults value in Swift and it supports the new privacy rules from apple.) api. it is good I like it. But I want to make it better by accessing the keys of the userDefaults through kaypath.

@UserDefault(\.testKey) var testValue = Data()
fileprivate extension DefaultKeys {
    static let testKey: String = ""
}

This is what I am trying to reach, this syntax. I know that we can’t refrence a static property using keyPath. This is why I came here to find some suggestion how to redo it to over come this and reach this syntax.

This is why I came here to find some suggestion how to redo it
to over come this and reach this syntax.

I’ve done this a bunch of times and I usually start out by stealing cribbing ideas from SwiftUI’s Environment property wrapper. Check out the text starting with “Create custom environment values” on this page.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like