美文网首页iOS开发与OC、Swift进阶
Swift中Runtime实现归解档

Swift中Runtime实现归解档

作者: 冲云简 | 来源:发表于2018-04-10 23:18 被阅读0次

    首先定义个名为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

    相关文章

      网友评论

        本文标题:Swift中Runtime实现归解档

        本文链接:https://www.haomeiwen.com/subject/pudohftx.html