用于 UserDefaults 的微库,支持 Codable。
CodableDefaults 使用 Swift 4 编写。
CodableDefaults 提供易于使用和类型安全。它支持 Swift 标准库和 Foundation 中的 Codable 类型。
let userID = DefaultStore<String>(forKey: "user ID")
userID.set("your ID") // sets to the UserDefaults.standard
userID.set(10) // ❗️compile error
userID.get() // -> "your ID"
只需遵循 Codable
struct User: Codable {
var name: String
var following: [User]
}
然后
let friend = User(name: "Friend", following: [])
let you = User(name: "You", following: [friend])
let user = DefaultStore<User>(forKey: "user")
user.set(you)
user.get() // -> User(name: "You", following: [friend])
使用初始化器包装 register(defaults:)
。
let shouldShowThumbnail = DefaultStore<Bool>(forKey: “shouldShowThumbnail”, defaultValue: true)
shouldShowThumbnail.get() // -> true
使用 UserDefaults
实例初始化。
let defaults = UserDefaults(suiteName: “domain name”)
let numbers = DefaultStore<[Int]>(forKey: “numbers”, userDefaults: defaults)
添加到你的 Cartfile
github "mt-hodaka/CodableDefaults"
添加到你的 Podfile
pod 'CodableDefaults'