Extending AppStorage to be compatible with Int32

Hello,

I would like to store CLAuthorizationStatus in the User Defaults using the AppStorage property wrapper, so that I can access it throughout the app as a simple variable rather than accessing the authorisation status every time via CLLocationManager which requires to import MapKit.

The problem is that this Enum rawValue Type is an Int32 (instead of Int, e.g. UNAuthorizationStatus), so the following does not work:

Code Block
@AppStorage("locationSettings") var locationSettings: CLAuthorizationStatus = CLAuthorizationStatus.notDetermined

throws: Candidate requires that the types 'CLAuthorizationStatus.RawValue' (aka 'Int32') and 'Int' be equivalent (requirement specified as 'Value.RawValue' == 'Int') (SwiftUI.AppStorage)

I tried extending AppStorage to include an initializer with a RawRepresentable that has Int32 as rawValue but I can't find what to include in it to make it work.

Code Block
extension AppStorage {
init(wrappedValue: Value, _ key: String, store: UserDefaults? = nil) where Value : RawRepresentable, Value.RawValue == Int32 {
}
}


Thanks in advance.

I tried extending AppStorage to include an initializer with a RawRepresentable that has Int32 as rawValue but I can't find what to include in it to make it work.

I cannot find any public APIs in AppStorage to implement another initializer. You may need another way around for this issue.
Code Block
@AppStorage("locationSettings") var rawLocationSettings: Int = Int(CLAuthorizationStatus.notDetermined.rawValue)
var locationSettings: CLAuthorizationStatus {
get {
return CLAuthorizationStatus(rawValue: Int32(rawLocationSettings))!
}
set {
rawLocationSettings = Int(newValue.rawValue)
}
}


Please share your solution when you find a better way.

Extending AppStorage to be compatible with Int32
 
 
Q