目录

CodableDefaults

Language Platform Build Status codecov Carthage compatible Version LICENSE

Micro library for UserDefaults with Codable.

Requirements

CodableDefaults is written Swift 4.

Usage

CodableDefaults provides easy to use, and type safety. It supports Codable types in Swift standard library and Foundation.

let userID = DefaultStore<String>(forKey: "user ID")

userID.set("your ID") // sets to the UserDefaults.standard
userID.set(10) // ❗️compile error

userID.get() // -> "your ID"

Custom Type

Just conform to the Codable:

struct User: Codable {
    var name: String
    var following: [User]
}

then:

let friend = User(name: "Friend", following: [])
let you = User(name: "You", following: [friend])

let user = DefaultStore<User>(forKey: "user")

user.set(you)

user.get() // -> User(name: "You", following: [friend])

Initial value

Use initializer wrapping register(defaults:).

let shouldShowThumbnail = DefaultStore<Bool>(forKey: “shouldShowThumbnail”, defaultValue: true)

shouldShowThumbnail.get() // -> true

With specified suite name

Init with UserDefaults instance.

let defaults = UserDefaults(suiteName: “domain name”)

let numbers = DefaultStore<[Int]>(forKey: “numbers”, userDefaults: defaults)

Installation

Carthage

Insert to your Cartfile:

github "mt-hodaka/CodableDefaults"

CocoaPods

Insert to your Podfile:

pod 'CodableDefaults'
关于
44.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802032778号