ios - UserDefault 属性包装器不保存值 iOS 版本低于 iOS 13
问题描述
我正在使用属性包装器来保存我的用户默认值。在 iOS 13 设备上,此解决方案效果很好。但是在 iOS 11 和 iOS 12 上,这些值不会保存到用户默认值中。我读到属性包装器是向后兼容的,所以我不知道为什么这在旧 iOS 版本上不起作用。
这是属性包装器:
@propertyWrapper
struct UserDefaultWrapper<T: Codable> {
private let key: String
private let defaultValue: T
init(key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
guard let data = UserDefaults.standard.object(forKey: key) as? Data else {
// Return defaultValue when no data in UserDefaults
return defaultValue
}
// Convert data to the desire data type
let value = try? JSONDecoder().decode(T.self, from: data)
return value ?? defaultValue
}
set {
// Convert newValue to data
let data = try? JSONEncoder().encode(newValue)
UserDefaults.standard.set(data, forKey: key)
UserDefaults.standard.synchronize()
}
}
}
struct UserDefault {
@UserDefaultWrapper(key: "userIsSignedIn", defaultValue: false)
static var isSignedIn: Bool
}
然后我可以像这样设置值:
UserDefault.isSignedIn = true
我是否使用了错误的属性包装器?是否还有其他人在旧 iOS 版本上遇到属性包装器问题?
解决方案
Nothing to do with property wrappers! The problem is that in iOS 12 and before, a simple value like a Bool (or String, etc.), though Codable as a property of a Codable struct (for example), cannot itself be JSON encoded. The error (which you are throwing away) is quite clear about this:
Top-level Bool encoded as number JSON fragment.
To see this, just run this code:
do {
_ = try JSONEncoder().encode(false)
print("succeeded")
} catch {
print(error)
}
On iOS 12, we get the error. On iOS 13, we get "succeeded"
.
But if we wrap our Bool (or String, etc.) in a Codable struct, all is well:
struct S : Codable { let prop : Bool }
do {
_ = try JSONEncoder().encode(S(prop:false))
print("succeeded")
} catch {
print(error)
}
That works fine on both iOS 12 and iOS 13.
And that fact suggests a solution! Redefine your property wrapper so that it wraps its value in a generic Wrapper struct:
struct UserDefaultWrapper<T: Codable> {
struct Wrapper<T> : Codable where T : Codable {
let wrapped : T
}
private let key: String
private let defaultValue: T
init(key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
guard let data = UserDefaults.standard.object(forKey: key) as? Data
else { return defaultValue }
let value = try? JSONDecoder().decode(Wrapper<T>.self, from: data)
return value?.wrapped ?? defaultValue
}
set {
do {
let data = try JSONEncoder().encode(Wrapper(wrapped:newValue))
UserDefaults.standard.set(data, forKey: key)
} catch {
print(error)
}
}
}
}
Now it works on iOS 12 and iOS 13.
By the way, I actually think you would do better to save as a property list rather than JSON. But that makes no difference to the question generally. You can’t encode a bare Bool as a property list either. You’d still need the Wrapper approach.
推荐阅读
- python - Flask 应用程序中可能的循环导入问题
- flutter - Flutter 可以选择在小部件树中包含/排除父小部件
- jquery - 加载时删除类和禁用属性
- sql-server - 我可以检查每个 ID 号的条件,然后在 T-SQL 中的新列中返回结果吗
- xml - XPath:如何将函数的返回值保存在变量中?
- azure-devops - 在 Azure DevOps Pipelines 中找不到管道向导
- jmeter - 无法使用 graphql 发送 Jmeter 请求,使用有效 URL 的 400 响应
- ssis - 如何获取 SSIS 包中的所有表名?
- html - Transparent Scrollbar Is Not Rendering Child Items Behind It
- reactjs - 带有反应钩子的值流