1

我按照本指南使用用户默认值创建包装属性:

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
     }

}
4

1 回答 1

2

好的,有两个变化:

struct Storage {
    @UserDefault(key: .isFirstLaunch)
    var isFirstLaunch: Bool?

    @UserDefault(key: .username)
    var username: String?
}

mutating func ex() {
    storage.username = "travis"
    storage.isFirstLaunch = true
}
于 2020-04-01T03:28:30.240 回答