💾
Storez安全、静态类型、存储无关的关键字存储
高亮
-
完全可定制
定制持久化存储、KeyType
类、提交后操作等,让这个框架成为您的专属! -
内置了电池:
如果要使用一些功能,该框架包含了一组预配置的基本类,您可以直接使用。 -
可移植性,检查!
如果您想在应用和扩展、watchkit、Apple Watch 之间共享代码,您就得到了支持!您可以使用NSUserDefaults
存储,只需要将共享容器标识符设置为套件名称。
示例
final class WeatherService {
private enum Keys: Namespace {
static let id = "weather-service"
static let temperature = Key<Keys, Double>(id: "temp", defaultValue: 0.0)
}
private let store: UserDefaultsStore
var temperature: Double {
return store.get(Keys.temperature)
}
init(store: UserDefaultsStore) {
self.store = store
}
func weatherUpdate(temperature: Double) {
store.set(Keys.temperature, temperature)
}
}
特性
可用存储
存储 | 后端 | 子规格 |
---|---|---|
UserDefaults存储 |
NSUserDefaults |
Storez/UserDefaults |
缓存存储 |
NSCache |
Storez/Cache |
对于所有存储,只需使用 pod "Storez"
类型安全、存储无关、可嵌套的关键定义
// Entries must belong to a "group", for namespacing
struct Animals: Namespace {
static let id = "animals"
}
let kingdom = Key<Animals, Void?>(id: "mammals", defaultValue: nil)
kingdom.stringValue // "animals:mammals"
// Nesting
struct Cats: Namespace {
typealias parent = Animals
static let id = "cats"
// Namespaces also have pre and post commit hooks
func preCommitHook() { /* custom code */ }
func postCommitHook() { /* custom code */ }
}
let cat = Key<Cats, Void?>(id: "lion", defaultValue: nil)
cat.stringValue // "animals:cats:lion"
初始化你想要的存储
// Use UserDefaultsStore for this example
let store = UserDefaultsStore(suite: "io.kitz.testing")
let key = Key<GlobalNamespace, Int?>(id: "key", defaultValue: nil)
// With three simple functions
store.set(key, value: 8)
store.get(key) // 8
store.clear() // Start fresh every time for testing
整个流程都尊重可选项
let nullable = Key<GlobalNamespace, String?>(id: "nullable", defaultValue: nil)
store.get(nullable)?.isEmpty // nil
store.set(nullable, value: "")
store.get(nullable)?.isEmpty // true
let nonnull = Key<GlobalNamespace, String>(id: "nonnull", defaultValue: "!")
store.get(nonnull).isEmpty // false
store.set(nonnull, value: "")
store.get(nonnull).isEmpty // true
支持自定义对象
新特性 简单符合 Codable
协议!
(如果需要,你仍然可以使用 UserDefaultsConvirtable
)
struct CustomObject: Codable {
var strings: [String]
}
// custom objects properly serialized/deserialized
let customObject = CustomObject(
strings: ["fill", "in", "the"]
)
// let's add a processing block this time
let CustomValue = Key<GlobalNamespace, CustomObject?>(id: "custom", defaultValue: nil) {
var processedValue = $0
processedValue?.strings.append("blank!")
return processedValue
}
store.set(CustomValue, value: customObject)
store.get(CustomValue)?.strings.joinWithSeparator(" ") // fill in the blank!
自定义你的 KeyType
// For example, make an key that emits NSNotifications
struct MyKey<G: Namespace, V>: KeyType {
typealias NamespaceType = G
typealias ValueType = V
var stringValue: String
var defaultValue: ValueType
func didChange(oldValue: ValueType, newValue: ValueType) {
NSNotificationCenter.defaultCenter().postNotificationName(stringValue, object: nil)
}
}
开始使用
使用 Carthage
Carthage 完全支持。只需在你的 Cartfile 中添加以下行
github "SwiftKitz/Storez"
使用 CocoaPods
CocoaPods 完全支持。你可以选择使用哪个存储(参见上文)。只需在你的 Podfile 中添加以下行
pod 'Storez/UserDefaults'
子模块
对于手动安装,你可以直接获取源代码,或通过 git 子模块,然后只需
- 将
Storez.xcodeproj
文件作为一个子项目拖入(确保没有启用Copy resources
) - 导航到您的根项目设置。在“嵌入式二进制文件”下,点击“+”按钮并选择
Storez.framework
动机
我看过许多优秀的静态类型数据存储尝试,但它们都构建了一个紧密耦合的设计,限制了最终开发者的自由。使用这个框架,您可以直接使用内置功能开始原型设计,然后随心所欲地替换持久存储和KeyType
功能,并且保持您的代码保持现状!
作者
Mazyod (@Mazyod)
许可证
Storez根据MIT许可证发布。详情请见LICENSE。