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")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()
}
}
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
}
- swift 本地归档、解档储存
- swift归档解档
- Swift-归档解归档
- swift实现归档解档
- Swift 解档和归档
- swift 归档解档操作
- iOS swift-归档与解档
- ios swift 归档解档遇到的坑 3-30
- swift中使用对象归档进行数据本地
- 归档-swift
- Xcode中怎么查找 创建本地、归档、解档文件?
- HTML5 localStorage本地储存
- SBT使用本地储存
- 本地储存NSUserDefaults
- 本地线程储存
- 本地线程储存
- unity PlayerPrefs本地储存
- HTML5本地储存
- ZOJ3623: Battle Ships(类完全背包)
- Pycharm 2016.3 注册码
- Oracle 数据库导入导出(imp/exp)
- 求各个位数的值相加起来的总和是多少
- 是时候搭建起自己的云盘服务了
- swift 本地归档、解档储存
- CMake 手册详解(八)
- Problem--4A--Codeforces--Watermelon
- jquery用法--css操作
- 飞机花费
- ios collectionView点击后只选中一个cell并改变样式
- 总结一下最短路径的贝尔曼-福特算法(Bellman-Ford)及用队列优化(spfa)
- easyui控件操作集锦
- iOS:关于iPhone连接Xcode时,RSA加密失败的一点总结