GithubHelp home page GithubHelp logo

Translate README to Chinese about defaultskit HOT 8 CLOSED

nmdias avatar nmdias commented on May 28, 2024 5
Translate README to Chinese

from defaultskit.

Comments (8)

linbx08 avatar linbx08 commented on May 28, 2024 36

什么是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.

nmdias avatar nmdias commented on May 28, 2024 9

I've added the Chinese translations kindly provided by @linbx08 to the README.zh-CN.md file. Thank you again!

from defaultskit.

linbx08 avatar linbx08 commented on May 28, 2024 5

hello, That was my understanding. hope can help you. thanks.

from defaultskit.

hewigovens avatar hewigovens commented on May 28, 2024 2

I made some refinements and here is the pull request: #3

from defaultskit.

nmdias avatar nmdias commented on May 28, 2024

Ah Fantastic! Thank you so much 🙏

from defaultskit.

dadpp avatar dadpp commented on May 28, 2024

厉害了 大佬

from defaultskit.

asd8855 avatar asd8855 commented on May 28, 2024

cool

from defaultskit.

Marcello168 avatar Marcello168 commented on May 28, 2024

It`s very nice

from defaultskit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.