项目简介
源码地址
SwiftUserDefault对系统的UserDefault进行了一层封装,可以简化基础数据和自定义对象的存取
使用方式
1、cocoapods集成,也可以直接将文件下载下来(就一个文件)
2、创建自己需要的DefualtKeys
extension DefaultsKeys {
static let keyString = DefaultsKey<String?>("keyString")
static let keyInt = DefaultsKey<Int>("keyInt")
static let keyBool = DefaultsKey<Bool?>("keyBool")
static let keyDic = DefaultsKey<[String: Any]?>("keyDic")
static let keyColor = DefaultsKey<UIColor?>("keyColor")
static let keyStudent = DefaultsKey<SXDefaultStudent?>("keyStudent")
}
其中SXDefaultStudent是自定义对象,自定义对象如果需要使用的话,需要完成归解档,如下:
class SXDefaultStudent: NSObject, NSCoding {
let keyCodeName = "keyCodeName"
let keyCodeAge = "keyCodeAge"
let keyCodeAddress = "keyCodeAddress"
var name: String?
var age: Int = -1
var address: SXDefaultAddress?
override init() {
super.init()
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: self.keyCodeName)
aCoder.encode(self.age, forKey: self.keyCodeAge)
aCoder.encode(self.address, forKey: self.keyCodeAddress)
}
required init?(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObject(forKey: self.keyCodeName) as? String
self.age = aDecoder.decodeInteger(forKey: self.keyCodeAge)
self.address = aDecoder.decodeObject(forKey: self.keyCodeAddress) as? SXDefaultAddress
}
override var description: String {
let str = self.name ?? ""
return str
}
}
class SXDefaultAddress: NSObject, NSCoding {
let keyCodeProvince = "keyCodeProvince"
let keyCodeStreet = "keyCodeStreet"
var province: String?
var street: String?
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
self.province = aDecoder.decodeObject(forKey: self.keyCodeProvince) as? String
self.street = aDecoder.decodeObject(forKey: self.keyCodeStreet) as? String
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.province, forKey: self.keyCodeProvince)
aCoder.encode(self.street, forKey: self.keyCodeStreet)
}
}
其中student中除了基础数据,还有一个自定义的address
3、定义下标访问方式,如果不定义,就不能通过下标的方式访问,只能使用下面的方式
Defaults.set(DefaultsKeys.keyStudent, student)
正确的方式
extension UserDefaults {
subscript(key: DefaultsKey<SXDefaultStudent?>) -> SXDefaultStudent? {
get { return unarchive(key) }
set { archive(key, newValue) }
}
subscript(key: DefaultsKey<[String: Any]?>) -> [String: Any]? {
get { return unarchive(key) }
set { archive(key, newValue) }
}
}
4、测试代码
// 自定义类型,类型中又包含自定义类型
let student = SXDefaultStudent()
student.name = "guandi"
student.age = 28
let addr = SXDefaultAddress()
addr.province = "hunan"
addr.street = "baoli"
student.address = addr
Defaults.set(DefaultsKeys.keyStudent, student)
Defaults[DefaultsKeys.keyStudent] = student
// 基础类型测试
Defaults[.keyString] = "keyStringTest"
Defaults[.keyInt] = 12
Defaults[.keyDic] = ["testKey": "testValue"]
Defaults[.keyDic]?["student"] = student
var str = ""
str.append(Defaults[.keyString] ?? "")
str.append("\n")
str.append("\(Defaults[.keyInt])")
str.append("\n")
print(str)
if let dic = Defaults[.keyDic], let value = dic["testKey"] {
print(value)
}
if let dic = Defaults[.keyDic], let value = dic["student"] {
print(value)
}