SwiftKeyValueStore
为键值存储数据库提供类型安全的 Swift API。
UserDefaults
和 SwiftKeychainWrapper
的扩展,提供简单、类型安全且富表达性的 Swifty API,同时具有静态类型的好处。选择您想使用的数据库类型 - 未加密的 UserDefaults
或密钥链中的加密存储。在一个地方定义您的键,轻松使用值类型,并获得额外的安全性和方便的编译时检查。
SwiftKeyValueStore 是对 版本 0.1
功能
使用SwiftKeyValueStore只需三个步骤
步骤 1: 选择存储类型 UserDefaults
。使用 standard
共享实例或创建新实例。
var DefaultsKeyValueStore = UserDefaults.standard
或者是密钥链中的加密存储。使用 standard
共享实例或创建新实例。
var KeychainKeyValueStore = KeychainWrapper.standard
步骤 2: 定义你的键。
extension KeyValueStoreKeys {
static let userName = KeyValueStoreKey<String>("UserNameKey")
static let onboardingIsEnabled = KeyValueStoreKey<Bool>("OnboardingIsEnabledKey")
}
步骤 3: 开始使用
//Set and get Keychain.
KeychainKeyValueStore[.userName] = "[email protected]"
let username = KeychainKeyValueStore[.userName]
//Set and get User defaults
DefaultsKeyValueStore[.onboardingIsEnabled] = true
// Modify value types in place
DefaultsKeyValueStore[.launchCount] += 1
// Use and modify typed arrays
DefaultsKeyValueStore[.movies].append("StarWars")
DefaultsKeyValueStore[.movies][0] += " Last Jedi"
// Works with types that conform Codable or NSCoding
DefaultsKeyValueStore[.color] = UIColor.white
DefaultsKeyValueStore[.color]?.whiteComponent // => 1.0
便捷的点语法只有在通过扩展 KeyValueStoreKeys
类定义键时才可用。或者只需在方括号中传递 KeyValueStoreKey
的值。或者使用字符串来创建具有指定 ValueTypte
或默认值的键。
使用方法
定义你的键
定义你的用户键以提高便利性
let userKey = KeyValueStoreKey<User>("userKey")
let colorKey = "ColorKey".toKeyWith(type: UIColor)
let profilesKey = "ProfilesKey".toKeyWith(defaultValue: Array<Profile>())
创建一个 KeyValueStoreKey
对象,在括号中提供你想要存储的值的类型和键名。或使用 String
扩展来从 String
创建方便的 KeyValueStoreKey
。
创建你的存储实例。您可以使用 UserDefault
存储或 KeyChainWrapper
存储。
var KeychainKeyValueStore = KeychainWrapper.standard
var DefaultsKeyValueStore = UserDefaults.standard
现在使用你的存储来访问这些值
// store in UserDefaults
DefaultsKeyValueStore[colorKey] = "red"
DefaultsKeyValueStore[colorKey] // => UIColor.red, typed as UIColor?
// store securely in KeyChain
KeychainKeyValueStore[userKey] = User(firstName: "Yuriy",
lastName: "Gagarin") // struct User has to conform `Codable` protocol
KeychainKeyValueStore[userKey] // => (firstName: "Yuriy",
// lastName: "Gagarin"), typed as User?
编译器不会允许你设置错误的数据类型,并且总是返回预期的不确定的类型。
支持的数据类型
SwiftKeyValueStore支持所有标准的NSUserDefaults
类型,如字符串、数字、布尔值、数组和字典。以及任何符合Codable或NSCoding协议的类型。
Codable
SwiftKeyValueStore
支持 Codable
。只需为你的类型添加符合 Codable
协议的支持,如下所示
struct User: Codable {
let firstName: String
let lastName: String
}
免费获取数组和字典的支持
let users = KeyValueStoreKey<[User]>("users")
NSCoding
SwiftKeyValueStore
支持 NSCoding
. 只需将 NSCoding
协议合规性添加到您的类型中,并实现必需的方法。
class UserProfileView: UIView, NSCoding {
let userID: String
init(frame: CGRect, id: String) {
self.userID = id
super.init(frame: frame)
}
override func encode(with aCoder: NSCoder) {
aCoder.encode(userID, forKey: "UserProfileView.Id")
super.encode(with: aCoder)
}
required init?(coder aDecoder: NSCoder) {
guard let id = aDecoder.decodeObject(forKey: "UserProfileView.Id") as? String else { return nil }
self.userID = id
super.init(coder: aDecoder)
}
}
默认值
let counter = KeyValueStoreKey<Int>("counterKey", defaultValue: 0)
let user = KeyValueStoreKey<User>("token", defaultValue: User(firstName: "Anakin",
lastName: "Skywalker"))
删除所有键
要重置用户默认值,请使用 resetStorage
方法。
DefaultsKeyValueStore.resetStorage()
共享用户默认值
如果您在不同应用程序或应用程序及其扩展之间共享用户默认值,您可以创建自己的 UserDefaults 或 KeyChainWrapper 实例。
var CustomSharedDefaults = UserDefaults(suiteName: "my.amazing.app")!
安装
CocoaPods
如果您正在使用 CocoaPods,只需将以下行添加到您的 Podfile。
pod 'SwiftKeyValueStore'
在终端运行以下命令进行安装。
pod install
然后,在所有使用它的文件中导入此库。
import SwiftKeyValueStore
SwiftKeyValueStore 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。