天天看點

swift3.0歸檔和解檔

1.對使用者的模型資料(自定義類:HCUserModel)進行歸檔和解檔

1.1 需要遵循NSCoding協定

1.2 需要實作func encode(with aCoder: NSCoder){}歸檔方法

1.3需要實作 required init(coder aDecoder: NSCoder){}解檔方法

1.4 重寫init方法

2.HCUserModel的資料内容如下:

import UIKit

class HCUserModel: NSObject,NSCoding {

    var id:Int?

    var nickname:String?

    var phone:String?

    var account:String?

    var password:String?

    var type:Int?

    var icon:String?

    var attentionnumber:Int?

    var registertime:String?

    var qrcode:String?

    var signature:String?

    var dynamicstruts:Int?

    var score:Int?

    // MARK:- 處理需要歸檔的字段

    func encode(with aCoder:NSCoder) {

        aCoder.encode(id, forKey:"id")

        aCoder.encode(nickname, forKey:"nickname")

        aCoder.encode(phone, forKey:"phone")

        aCoder.encode(account, forKey:"account")

        aCoder.encode(password, forKey:"password")

        aCoder.encode(type, forKey:"type")

        aCoder.encode(icon, forKey:"icon")

        aCoder.encode(attentionnumber, forKey:"attentionnumber")

        aCoder.encode(registertime, forKey:"registertime")

        aCoder.encode(qrcode, forKey:"qrcode")

        aCoder.encode(signature, forKey:"signature")

        aCoder.encode(dynamicstruts, forKey:"dynamicstruts")

        aCoder.encode(score, forKey:"score")

    }

    // MARK:- 處理需要解檔的字段

    requiredinit(coder aDecoder:NSCoder) {

        super.init()

        id = aDecoder.decodeObject(forKey:"id")as?Int

        nickname = aDecoder.decodeObject(forKey:"nickname")as?String

        phone = aDecoder.decodeObject(forKey:"phone")as?String

        account = aDecoder.decodeObject(forKey:"account")as?String

        password = aDecoder.decodeObject(forKey:"password")as?String

        type = aDecoder.decodeObject(forKey:"type")as?Int

        icon = aDecoder.decodeObject(forKey:"icon")as?String

        attentionnumber = aDecoder.decodeObject(forKey:"attentionnumber")as? Int

        registertime = aDecoder.decodeObject(forKey:"registertime")as?String

        qrcode = aDecoder.decodeObject(forKey:"qrcode")as?String

        signature = aDecoder.decodeObject(forKey:"signature")as?String

        dynamicstruts = aDecoder.decodeObject(forKey:"dynamicstruts")as? Int

        score = aDecoder.decodeObject(forKey:"score")as?Int

    }

    overrideinit() {

        super.init()

    }

}

3. 實作歸檔把模型儲存到本地Document檔案夾: 3.1 擷取本地Document路徑,一般路徑都設為全局變量,友善解檔直接使用:

let userAccountPath ="\(NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,

FileManager.SearchPathDomainMask.userDomainMask,true).first!)/userAccount.data"

3.2 對擷取到的模型進行歸檔操作,要注意模型必須是确定的類型,如果是可選類型會報發送未識别的消息的錯誤(切記)

NSKeyedArchiver.archiveRootObject(userModel!, toFile:userAccountPath)

4.實作解檔從Document檔案夾擷取本地模型資料

4.1 判斷Document檔案夾下是否有已儲存好的模型,有的話就解檔取出模型

 if NSKeyedUnarchiver.unarchiveObject(withFile:userAccountPath) !=nil {

            userModel =NSKeyedUnarchiver.unarchiveObject(withFile:userAccountPath)as? HCUserModel

        }