前言
- 在OC工程中,如果存储自定义对象,可以用系统自带的解档,归档,把自定义对象转换成NSData的形式,完成存储,也可以用第三方库 方便的进行存储,如:YYCache
- 在Swift工程,同时也可以用系统自带的方法,如果想方便的进行存储,也可以借助第三方库来完成,如:Cache
- 下面主要陈述Cache第三方库的使用
正题
- 第一步,找到第三方,上面已经有链接了
- 导入到工程中,可以用pod的形式,也可以用直接将源码文件拖到自己的工程里面(本人使用了后者)
-
开始使用
image.png
第一步,初始化storage这个类
因为这个类的初始化工作,有点复杂,代码量有点多,所以想办法,把它封装一下
新建一个CacheTool工具类
class CacheTool: NSObject {
class func cacheTool() -> Storage<String, [Item]> {
let diskConfig = DiskConfig(name: "smartLight")
let memoryConfig = MemoryConfig(expiry: .never, countLimit: 100, totalCostLimit: 100)
let storage = try? Storage<String, [Item]>(
diskConfig: diskConfig,
memoryConfig: memoryConfig,
transformer: TransformerFactory.forCodable(ofType: [Item].self) // Storage<String, Item>
)
return storage!
}
}
其中Item
是将要存储对象的类名,smartLight
是存储的文件名称,上面直接写死的,也可以通过参数让外界传过来
这样就可以方便的获取到存储对象storage
第二步:构建要存储的对象(对象里面还嵌套对象数组)
class Book: NSObject, Codable {
var name: String
var price: Double
init(name: String,price:Double) {
self.name = name
self.price = price
}
}
class Item: NSObject,Codable {
var uuid: UUID
var inAction: Int
var outAction: Int
var book: [Book]
init(uuid: UUID,inAction:Int,outAction:Int,book:[Book]) {
self.book = book
self.uuid = uuid
self.inAction = inAction
self.outAction = outAction
}
}
** 对象需要遵守Codable协议**
开始存储
let storage = CacheTool.cacheTool()
let book1 = Book(name: "Dart", price: 20.0)
let item1 = Item(uuid: UUID(uuidString: "00000023-0000-1000-8000-0026BB765200")!, inAction: 100, outAction: 99, book: [book1])
let book2 = Book(name: "Flutter", price: 30.0)
let book3 = Book(name: "Harmony", price: 40.0)
let item2 = Item(uuid: UUID(uuidString: "00000023-0000-1000-8000-0026BB765299")!, inAction: 1, outAction: 0, book: [book2,book3])
let items = [item1,item2]
try? storage.setObject(items, forKey: "myItem")
获取已存储的数据
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let storage = CacheTool.cacheTool()
let entry = try? storage.entry(forKey: "myItem")
let items = entry?.object
let firstItem = items?.last
guard let firstObject = firstItem else { return }
print("item \(firstObject.book)")
print("item \(firstObject.uuid)")
print("item \(firstObject.inAction)")
print("item \(firstObject.outAction)")
}
打印结果如下
item [<CacheObjectList.Book: 0x2825d2b20>, <CacheObjectList.Book: 0x2825d2c00>]
item 00000023-0000-1000-8000-0026BB765299
item 1
item 0

全部流程完成
完整代码
import UIKit
class Book: NSObject, Codable {
var name: String
var price: Double
init(name: String,price:Double) {
self.name = name
self.price = price
}
}
class Item: NSObject,Codable {
var uuid: UUID
var inAction: Int
var outAction: Int
var book: [Book]
init(uuid: UUID,inAction:Int,outAction:Int,book:[Book]) {
self.book = book
self.uuid = uuid
self.inAction = inAction
self.outAction = outAction
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let storage = CacheTool.cacheTool()
let book1 = Book(name: "Dart", price: 20.0)
let item1 = Item(uuid: UUID(uuidString: "00000023-0000-1000-8000-0026BB765200")!, inAction: 100, outAction: 99, book: [book1])
let book2 = Book(name: "Flutter", price: 30.0)
let book3 = Book(name: "Harmony", price: 40.0)
let item2 = Item(uuid: UUID(uuidString: "00000023-0000-1000-8000-0026BB765299")!, inAction: 1, outAction: 0, book: [book2,book3])
let items = [item1,item2]
try? storage.setObject(items, forKey: "myItem")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let storage = CacheTool.cacheTool()
let entry = try? storage.entry(forKey: "myItem")
let items = entry?.object
let firstItem = items?.last
guard let firstObject = firstItem else { return }
print("item \(firstObject.book)")
print("item \(firstObject.uuid)")
print("item \(firstObject.inAction)")
print("item \(firstObject.outAction)")
}
}
结尾
今天iOS 相关技术的分享就到这里喽,小伴们,觉得有点用的话,或者已经看到这里面来的请点个赞吧~~ 后续分享更多有关iOS的文章。如果有疑问的话,请在下方留言~
网友评论