pod 'ObjectMapperCacheManager'
把数据存到设备上时,key
是唯一标识,与UserDefaults
使用方法类似。
UserDefaults.standard.setValue(["name": "James", "age": 16], forKey: "User")
与UserDefaults
唯一的不同之处在于存放在沙盒中的路径:
UserDefaults
的存放路径为Home/Library/Preference/
ObjectMapperCacheManager
的存放路径为Home/Library/Caches
数据格式可以是字典或数组:
let dict: [String: Any] = ["name": "James", "age": 16]
CacheManager.setCache(json: dict, for: "Json")
let dictArray = [[String: Any]] = [dict]
CacheManager.setCache(json: dictArray, for: "JsonArray")
// 需显式声明类型
if let dict: [String: Any] = CacheManager.cacheJson(for: "Json") as? [String: Any]{
//print(dict)
}
if let array: [[String: Any]] = CacheManager.cacheJson(for: "JsonArray") as? [String: Any]{
//print(array)
}
储存对象的类型需遵从ObjectMapper
中的Mapple
协议:
let user: User = User()
CacheManager.setCache(object: user, for: "Object")
let userList = [user]
CacheManager.setCache(array: userList, for: "ObjectArray")
// 需显式声明类型
if let user: User = CacheManager.cache(for: "Object") {
//Do Something
}
if let array: [User] = CacheManager.cacheArray(for: "ObjectArray") {
//Do Something
}
ObjectMapperCacheManager
遵循 MIT
开源协议, 详见License.