Swift中自定義model的歸檔

首先對于歸檔的定義就不再贅述

最近在做一個(gè)項(xiàng)目,里面有一個(gè)功能是把購物車?yán)锏漠a(chǎn)品緩存到本地,由于數(shù)據(jù)量較少,選擇歸檔模式。

首先看一下數(shù)據(jù)模型。SSTCart對應(yīng)的是購物車的model,里面包含兩個(gè)元素,一個(gè)是list數(shù)組,用來存放產(chǎn)品,一個(gè)是購物車所有產(chǎn)品的totalprice。list數(shù)組里面的的產(chǎn)品對應(yīng)的model是SSTOrderItem

步驟一:對SSTCart的model進(jìn)行encode

  • 遵從NSCoding協(xié)議
  • 實(shí)現(xiàn)coding協(xié)議
    required init?(coder aDecoder: NSCoder) {
    super.init()
    list = aDecoder.decodeObjectForKey("List") as! [SSTOrderItem]
    totalPrice = aDecoder.decodeDoubleForKey("TotalPrice")
    }
    override init() {
    }
    func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(list, forKey: "List")
    aCoder.encodeDouble(totalPrice, forKey: "TotalPrice")
    }

步驟二:對SSTOrderItem的model進(jìn)行encode

  • 遵從NSCoding協(xié)議

  • 實(shí)現(xiàn)coding協(xié)議
    required init?(coder aDecoder: NSCoder) {
    super.init()
    cartId = aDecoder.decodeObjectForKey("CartId") as! NSNumber
    userId = aDecoder.decodeObjectForKey("UserId") as! NSNumber
    productId = aDecoder.decodeObjectForKey("ProductId") as! NSNumber
    unitPrice = aDecoder.decodeDoubleForKey("UnitPrice")
    createdate = aDecoder.decodeObjectForKey("Createdate") as! String
    qtyPrice = aDecoder.decodeDoubleForKey("QtyPrice")
    qty = aDecoder.decodeIntegerForKey("Qty")
    originUnitPrice = aDecoder.decodeDoubleForKey("OriginUnitPrice")
    imagePath = aDecoder.decodeObjectForKey("ImagePath") as! String
    productName = aDecoder.decodeObjectForKey("ProductName") as! String
    savedMoney = aDecoder.decodeDoubleForKey("SavedMoney")

    }
    override init() {
      
    }
    func encodeWithCoder(aCoder: NSCoder) {
      aCoder.encodeObject(cartId, forKey: "CartId")
      aCoder.encodeObject(userId, forKey: "UserId")
      aCoder.encodeObject(productId, forKey: "ProductId")
      aCoder.encodeDouble(unitPrice, forKey: "UnitPrice")
      aCoder.encodeObject(createdate, forKey: "Createdate")
      aCoder.encodeDouble(qtyPrice, forKey: "QtyPrice")
      aCoder.encodeInteger(qty, forKey: "Qty")
      aCoder.encodeDouble(originUnitPrice, forKey: "OriginUnitPrice")
      aCoder.encodeObject(imagePath, forKey: "ImagePath")
      aCoder.encodeObject(productName, forKey: "ProductName")
      aCoder.encodeDouble(savedMoney, forKey: "SavedMoney")
    }
    

這樣兩個(gè)model的encoding算是實(shí)現(xiàn)了

步驟三:歸檔和解檔的方法

自己寫了一個(gè)文件的讀寫的class
在這個(gè)class里面的代碼如下:
static func getFilePath(filePath: String) -> String? {

    var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.AllDomainsMask, true)
    if paths.count > 0 {
        return "\(paths[0])/"+filePath
    }
    
    return nil
  }
    //MARK: 歸檔的方法
  static func archive(fileName: String, object: NSObject) -> Bool {
    let name = getFilePath(fileName)!
    return NSKeyedArchiver.archiveRootObject(object, toFile: name )
  }
  //MARK: 解檔的方法
  static func unarchive(fileName: String) -> AnyObject? {
    return NSKeyedUnarchiver.unarchiveObjectWithFile(getFilePath(fileName)!)
  }

步驟四:把請求得到的購物車數(shù)據(jù)進(jìn)行歸檔

代碼如下:

    if FileOP.archive(kShoppingCartFileName, object: tmpSelf!.shoppingCart){
         print("add to file success")
     } else {
         print("add to file failure!")
     }             

其中,tmpSelf!.shoppingCart是從網(wǎng)絡(luò)請求之后得到的數(shù)據(jù)。

至此,歸檔操作已經(jīng)完成,如果需要解檔操作的話,直接調(diào)用 unarchiver方法即可。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容