扩展 AppStorage 以兼容 Int32

问题描述

我想使用 AppStorage 属性包装器将 CLAuthorizationStatus 存储在用户认值中,以便我可以在整个应用程序中将其作为简单变量进行访问,而不是每次都通过 CLLocationManager 访问授权状态。 问题是这个 Enum rawValue 类型是一个 Int32(而不是像 UNAuthorizationStatus 那样的 Int),所以以下不起作用:

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

throws: Candidate 要求类型 'CLAuthorizationStatus.RawValue'(又名 'Int32')和 'Int' 是等价的(要求指定为 'Value.RawValue' == 'Int')(SwiftUI.AppStorage)

我尝试扩展 AppStorage 以包含一个初始化程序,其中包含一个 RawRepresentable,它具有 Int32 作为 rawValue,但我找不到要包含在其中的内容以使其工作。

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

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)