Comments (8)
什么是DefaultsKit
DefaultsKit 是利用Swift4强大的Codable,在UserDefaults基础上提供一层简单且强大的封装。它仅使用少于70行代码来实现这些功能。
使用说明
第一步:
实例化或者使用单例来初始化
let defaults = Defaults()
// 或者使用 let defaults = Defaults.shared
第二步:
// 定义一个键(key)
let key = Key<String>("someKey")
// 设置值(value)
defaults.set("Codable FTW 😃", for: key)
// 通过设置的key来查看返回的value结果值
defaults.get(for: key) // 输出: Codable FTW 😃
判断某个键(key)是否含有某个值(value)
if defaults.has(key) {
// 书写你自己的代码
}
如果你只需要知道该键值队(key,value)是否存在,而不需要使用该key使用的返回值(value),使用has()这个方法代替这个可选的get(for:key)方法。对于一些复杂的对象,它可以减少避免一些不必要的转换
复杂的对象存储
一个遵循Codable协议的复杂对象需要存储
struct Person: Codable {
let name: String
let age: Int
}
// 然后创建一个键(key)
let key = Key<Person>("personKey")
// 获取一个实例符合Codable协议的枚举,结构体或者类
let person = Person(name: "Bonnie Greenwell", age: 80)
// 赋值
defaults.set(person, for: key)
// 最后查看key返回值的value
let person = defaults.get(for: key)
person?.name // Bonnie Greenwell
person?.age // 80
嵌套对象的存储
只要是遵循Codable协议,你也可以使用嵌套对象
enum Pet: String, Codable {
case cat
case dog
}
struct Person: Codable {
let name: String
let pets: [Pet]
}
// 获取一个遵循Codable协议的实例
let person = Person(name: "Claire", pets: [.cat])
// 赋值
defaults.set(person, for: key)
// 查看key返回值的value
let person = defaults.get(for: key)
person?.name // Claire
person?.pets.first // cat
from defaultskit.
I've added the Chinese translations kindly provided by @linbx08 to the README.zh-CN.md file. Thank you again!
from defaultskit.
hello, That was my understanding. hope can help you. thanks.
from defaultskit.
I made some refinements and here is the pull request: #3
from defaultskit.
Ah Fantastic! Thank you so much 🙏
from defaultskit.
厉害了 大佬
from defaultskit.
cool
from defaultskit.
It`s very nice
from defaultskit.
Related Issues (20)
- memory leak HOT 1
- only support swift4? HOT 1
- Support Implicit Member Expression HOT 1
- How to read it out from different View Controller? HOT 4
- Missing DefaultsKey HOT 6
- why type of Key class must be generic? HOT 2
- blob HOT 1
- DefaultsKit does not handle arguments passed on launch
- Swift.EncodingError.Context(codingPath: [], debugDescription: "Top-level Data encoded as string JSON fragment.", underlyingError: nil) HOT 1
- Swift5.1 support HOT 1
- The array object
- Test has function on simulator iOS10.3.1 always true
- Xcode 12.0.1 & SPM HOT 3
- how to add default value to Keys?
- how to initialise default with suitname ?
- Build failed with Xcode14.3
- Storing array of complex objects
- how to support array or dict? HOT 2
- Redundant conformance constraint HOT 4
- How to extend Key class Key with keys? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from defaultskit.