如何持久化和加载一个符合NSCoding协议的对象?
我创建了一个符合 NSCoding 协议的类,并执行所有编码和解码工作。
对于我的应用程序,我只想将该类中的一个对象保留到设备,并且下次应用程序启动时,我想将该对象加载回内存中。
基本上它只是一个保存一些用户输入信息的类。例如,用户开始编写文本,但随后退出应用程序。下次我想加载该数据对象。
我想我需要 NSKeyedArchiver?有关于这方面的好的教程吗?我该怎么做?
I have made an class which conforms to the NSCoding protocol and does all the encode and decode stuff.
For my app, I simply want to persist an object from that class to the device and the next time when the app launches, I want to load that object back into memory.
Basically it's just an class which holds some user input information. For example the user starts writing a text but then quits the app. Next time I want to load that data object.
I guess I need NSKeyedArchiver? Is there a good tutorial on this? How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您需要 NSKeyed(Un)Archiver。这两个类可以将您的对象与 NSData 进行转换,您可以将其保存/加载为文件。
要将对象保存到文件:
要从文件读取对象:
Yes you need NSKeyed(Un)Archiver. These 2 classes can convert your object to/from NSData which you can save/load it as a file.
To save your object to a file:
To read the object from a file: