KeyValueStorage
一款优雅、快速、线程安全、多用途的键值存储,兼容所有 Apple 平台。
支持的平台
iOS | macOS | watchOS | tvOS |
---|---|---|---|
9.0+ | 10.10+ | 2.0+ | 9.0+ |
安装
Swift Package Manager
Swift Package Manager 是一个自动化分布 Swift 代码的工具,它集成到 swift
编译器中。
一旦配置了你的 Swift 软件包,将 KeyValueStorage 添加为依赖项就像将其添加到 Package.swift
的 dependencies
值一样简单。
dependencies: [
.package(url: "https://github.com/narek-sv/KeyValueStorage.git", .upToNextMajor(from: "1.0.1"))
]
或者
- 在 Xcode 中选择 文件 > 添加包。
- 输入此项目的 URL: https://github.com/narek-sv/KeyValueStorage.git
在任何你想使用 KeyValueStorage 的文件中,别忘了使用 import KeyValueStorage
导入框架。
CocoaPods
CocoaPods 是针对 Cocoa 项目的依赖项管理器。有关使用和安装说明,请访问其网站。要使用 CocoaPods 将 KeyValueStorage 集成到你的 Xcode 项目中,请在 Podfile
中指定它。
pod 'KeyValueStorageSwift'
然后运行 pod install
。
在任何你想使用 KeyValueStorage 的文件中,别忘了使用 import KeyValueStorageSwift
导入框架。
用法
主要功能
首先,初始化存储
let storage = KeyValueStorage()
然后声明键,指定键名和项目的类型
let key = KeyValueStorageKey<Int>(name: "myAge")
之后,你可以进行保存
// Saves the item and associates it with the key or overrides the value if there is already such item.
let myAge = 21
storage.save(myAge, forKey: key)
检索
// Fetches and returns the item associated with the key or returns nil if there is no such item.
let fetchedAge = storage.fetch(forKey: key)
删除
// Deletes the item associated with the key or does nothing if there is no such item.
storage.delete(forKey: key)
设置
// Sets the item identified by the key to the provided value.
let newAge = 24
storage.set(newAge, forKey: key) // save
storage.set(nil, forKey: key) // delete
或清除整个存储内容
storage.clear()
KeyValueStorage
可与符合 Codable
协议的任何类型一起使用。
存储类型
KeyValueStorage 支持 3 种存储类型
内存
(此存储类型仅在应用程序会话中持久化项。)用户默认值
(此存储类型在整个应用程序生命周期内持久化项。)密钥链
(此存储类型将项保存在安全存储中,即使在应用程序重新安装后也会持续存在。支持iCloud
同步。)
您在声明键时指定存储类型。
let key1 = KeyValueStorageKey<Int>(name: "id", storage: .inMemory)
let key2 = KeyValueStorageKey<Date>(name: "birthday", storage: .userDefaults)
let key3 = KeyValueStorageKey<String>(name: "password", storage: .keychain())
如果您没有指定存储类型,将使用 .userDefaults
。
Xcode 自动补全
要获得 Xcode 自动补全的优点,建议将所有键在 KeyValueStorageKey
的扩展中声明,如下所示
extension KeyValueStorageKey {
static var key1: KeyValueStorageKey<Int> {
.init(name: "id", storage: .inMemory)
}
static var key2: KeyValueStorageKey<Date> {
.init(name: "birthday", storage: .userDefaults)
}
static var key3: KeyValueStorageKey<String> {
.init(name: "password", storage: .keychain())
}
}
然后 Xcode 将在您放置一个点时建议扩展中指定的所有键:
应用程序组
KeyValueStorage
还支持与共享容器一起使用,这使得您可以在不同的 应用程序扩展 或 您的其他应用程序 之间共享项。为此,首先,您需要按照本文中描述的步骤配置应用程序。
然后您只需使用init(accessGroup:teamID:)
初始化程序初始化您的KeyValueStorage
,并通过提供您的创建好的accessGroup
标识和开发teamID
来完成初始化。这就完成了;您现在可以使用应用组了。
Keychain
使用accessibility
参数指定钥匙串存储的安全性级别。默认使用.whenUnlocked
选项。这是最限制性的选项之一,提供了良好的数据保护。
let key = KeyValueStorageKey<String>(name: "password", storage: .keychain(accessibility: .whenUnlocked))
如果您的应用需要在后台访问钥匙串项目,可以使用.afterFirstUnlock
。请注意,这比.whenUnlocked
选项的安全性要低。
以下是所有支持的访问类型
- afterFirstUnlock
- afterFirstUnlockThisDeviceOnly
- whenPasscodeSetThisDeviceOnly
- whenUnlocked
- whenUnlockedThisDeviceOnly
将synchronizable
属性设置为true
以启用用户多设备上的钥匙串项同步。这种同步将适用于已在设备上的
let key = KeyValueStorageKey<String>(name: "password", storage: .keychain(accessibility: .afterFirstUnlock, isSynchronizable: true))
许可证
有关更多信息均参见License.md。