RxUserDefaults
概述
响应性的 UserDefaults,灵感来自 rx-preferences。
用法
要创建一个设置,请使用该类的构造函数
let settings = RxSettings(userDefaults: userDefaults)
let setting = settings.setting(key: "INSERT_KEY", defaultValue: "DEFAULT")
参数应该是自解释的。
目前只支持以下类型
- String
- Int
- Bool
- Array(仅支持 USD default 支持的类型)
- Enum(但枚举必须符合 RxSettingEnum 协议)
你可以使用的函数
// gets the value
let val = setting.value
// sets the value
setting.value = val
// check if value is saved (note: the default value is not automatically saved)
setting.isSet
// deletes the value
setting.remove()
// provides a hot observable that triggers on every change
// and starts with the current value (or default value)
setting.asObservable()
存储层
如果你不想使用UserDefaults作为存储层,你可以实现自己的。为此,你必须遵守StorageLayer协议。
警告 & TODOs
我希望支持UserDefaults支持的所有内容(例如,字典、URL...)。目前,你可以通过遵守RxSettingCompatible协议来扩展库以支持更多类型。但是请注意,持久化UserDefaults不支持的数据类型将静默失败。
示例
要运行示例项目,请先克隆存储库,然后从示例目录运行pod install
。
安装
RxUserDefaults可以通过以下方式获取:
- Carthage 安装时,只需将以下行添加到Cartfile文件中即可
github "num42/RxUserDefaults"
- CocoaPods 安装时,只需将以下行添加到Podfile文件中即可
pod "RxUserDefaults"
系统要求
项目
使用xcodegen生成xcodeproj文件。
要从project.yml更新它,请运行
xcodegen
或者如果您正在使用mint
mint run yonaskolb/XcodeGen
作者
David Kraus, [email protected]
Hans-Martin Schuller, [email protected]
Wolfgang Lutz, [email protected]
许可证
Copyright 2018 Number42 GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.