美文网首页
Swift 存储自定义对象数组

Swift 存储自定义对象数组

作者: 微风_10a5 | 来源:发表于2022-09-03 12:42 被阅读0次

前言

  • 在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
image.png

全部流程完成

完整代码


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的文章。如果有疑问的话,请在下方留言~

相关文章

  • Swift 存储自定义对象数组

    前言 在OC工程中,如果存储自定义对象,可以用系统自带的解档,归档,把自定义对象转换成NSData的形式,完成存储...

  • Swift 与 Objective-C 2.0 区别记录

    数组OC: NSArray 只可以存储对象,对象类型可以不同如[NSNumber, NSString]Swift:...

  • iOS Swift 字典转模型 模型存沙盒

    前言: 本篇接着上一篇iOS Swift 原生 字典数组转模型 JSONDecoder 对象存储 NSKeyedA...

  • 对象存储 - Swift

    提供对象存储服务的Swift介绍 1.什么是Swift? Swift 是 提供高可用分布式对象存储的服务,为nov...

  • swift 中的Array和ArraySlice

    1.swift的存储类型(所有类型,int, double, String······) Swift 数组用...

  • 【Java基础】- 集合

    对象数组 数组既可以存储基本数据类型,也可以存储引用类型。它存储引用类型的时候的数组就叫对象数组。 比如:用数组存...

  • oc 基础--NSArray

    // oc 的数组可以存储不同类型的对象// oc 的数组只能存储对象 NSNumber NSValue

  • iOS 中NSArray

    //OC 的数组 可以存储不同类型的对象 ,OC 的数组只能存储对象 //不可变数组 NSArray *array...

  • iOS归档看这篇就够了

    归档的作用 之前将数据存储到本地,只能是字符串、数组、字典、NSNuber、BOOL等容器类对象,不能将自定义对象...

  • Swift学习之十一:数组与字典(Array and Dicti

    /* Swift 提供了两种集合类型,即数组(Array)和字典(Dictionary),存储值的集合 数组存储相...

网友评论

      本文标题:Swift 存储自定义对象数组

      本文链接:https://www.haomeiwen.com/subject/dlyunrtx.html