import SwiftUI
@propertyWrapper
struct Wrap {
var wrappedValue: Int
}
extension Wrap: RawRepresentable {
var rawValue: String {
wrappedValue.description
}
init?(rawValue: String) {
guard let value = Int(rawValue) else {
return nil
}
wrappedValue = value
}
}
struct Test {
@AppStorage("x") @Wrap var x: Int = 5
}
var test = Test()
test.x = 6
print(UserDefaults.standard.object(forKey: "x")) // wonderful: it worked
print(type(of: test.$x)) // problem: this is `Binding<Wrap>`, how to access `Binding<Int>` instead?
// in my actual code, I'm using this solution to work with `Color` like this
// `@AppStorage("myColor") @ColorRawRepresentable var color = Color.blue`
// I need to get `Binding<Color>` to pass into `ColorPicker`
Ah. I think you’ll need to project the binding in that case ($x.wrappedValue). AppStorage doesn’t know about any additional nesting, so it has to vend what it’s given as its generic argument. That’s back to your “manual wrapper struct”, I know, but only in this case.