swift 本地归档、解档储存

来源:互联网 发布:金融数据挖掘python 编辑:程序博客网 时间:2024/06/06 09:06

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")asInt

        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")asInt

        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)asHCUserModel

        }


0 0