我遵循下面的指南来创建具有用户默认值的包装属性:
https://www.vadimbulavin.com/advanced-guide-to-userdefaults-in-swift/
现在,我有了LocalStorage类,它将在本教程结束时使用这个"Storage()“类,然后访问该对象的引用以从UserDefaults值中获取数据。由于某些原因,当我在代码中设置一个字符串时,它不再构建/编译。这对布尔人是有效的。下面是一个示例:
// The marker protocol
protocol PropertyListValue {}
extension Data: PropertyListValue {}
extension String: PropertyListValue {}
extension Date: PropertyListValue {}
extension Bool: PropertyListValue {}
extension Int: PropertyListValue {}
extension Double: PropertyListValue {}
extension Float: PropertyListValue {}
// Every element must be a property-list type
extension Array: PropertyListValue where Element: PropertyListValue {}
extension Dictionary: PropertyListValue where Key == String, Value: PropertyListValue {}
struct Key: RawRepresentable {
let rawValue: String
}
extension Key: ExpressibleByStringLiteral {
init(stringLiteral: String) {
rawValue = stringLiteral
}
}
extension Key {
static let isFirstLaunch: Key = "isFirstLaunch"
static let username: Key = "username"
}
@propertyWrapper
struct UserDefault<T: PropertyListValue> {
let key: Key
var wrappedValue: T? {
get { UserDefaults.standard.value(forKey: key.rawValue) as? T }
set { UserDefaults.standard.set(newValue, forKey: key.rawValue) }
}
}
struct Storage {
@UserDefault(key: .isFirstLaunch)
var isFirstLaunch: Bool
@UserDefault(key: .username)
var username: String
}
struct LocalStorage{
var storage: Storage
init(storage: Storage){
self.storage = storage
}
func ex(){
storage.username = "travis" // will not compile... "Abort 6"
storage.isFirstLaunch= true // without line above, it will compile
}
}发布于 2020-04-01 11:28:30
好的,有两个变化:
struct Storage {
@UserDefault(key: .isFirstLaunch)
var isFirstLaunch: Bool?
@UserDefault(key: .username)
var username: String?
}和
mutating func ex() {
storage.username = "travis"
storage.isFirstLaunch = true
}https://stackoverflow.com/questions/60962475
复制相似问题