首先定义个名为PersonModel类,在类中实现NSCoding方法
class PersonModel:NSObject,NSCoding
然后在开始使用Archiver进行数据持久化存本地
//路径
let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last
let filePath = path! +"/xxx.plist"
let model = PersonModel()
model.name="Joyce"
model.sex="man"
model.age=26
//归档
NSKeyedArchiver.archiveRootObject(model, toFile: filePath)
//解档
let readModel =NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as?PersonModel
网友评论