属性列表应该由它自己的模型对象管理吗?
我正在构建一个 iPhone 应用程序,其中通过属性列表包含一些默认数据。该数据是我的模型的来源。管理此数据以便用户可以从此 plist 读取(并且在某些情况下写入)数据的最佳方法是什么?
我目前正在对 NSObject 进行子类化,并将 plist 数据映射到该对象中的属性,并使用将数据读/写到该对象的方法。我已经阅读了 NSCoding 协议和 NSCoder,但不确定如何在我的自定义类中实现它。
任何帮助将不胜感激。
I am building an iPhone app that has some default data inside it via a property list. This data is the source for my Model. What is the best way to manage this data so the user can read (and in a couple of cases write) data from this plist?
I currently am subclassing NSObject and mapping the plist data to properties in that object, with methods to read/write data to the object. I have read about the NSCoding protocol and NSCoder but am not sure how to implement this in my custom class.
Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎答案就在这个链接中:
http://mojomonkeycoding.com/tag/nscoding/
我我猜你在这些情况下并不担心 super 被 initWithCoder 。
Seems the answer is in this link:
http://mojomonkeycoding.com/tag/nscoding/
I guess you do not worry about super being initWithCoder in these cases.
如果你确实想将数据保存在 plist 中,那么你可以使用 NSString:propertyList。它将获取属性列表并将其解析为您所需的结构。然后您可以使用 NSPropertyListSerialization 将其写回。
坦率地说,除非您有大量不同的实体需要跟踪,否则您所做的事情会更容易。如果您只感兴趣的话,有很多方法可以简单地加载和保存数据。除了 NSCoder(根据我的口味,这是很多样板代码)之外,您还可以使用 CoreData,而根本不用担心序列化过程 - -CoreData 半自动地为您管理这一切。
If you really want to keep the data in a plist then you can use NSString:propertyList. It'll take the property list and parse it into the necessary structures for you. You can then use NSPropertyListSerialization to write it back out.
Frankly, what you're doing is easier unless you have a ton of different entities to track. There are a lot of ways to simply load and save data, if that's all you're interested in. Besides NSCoder (which is a lot of boilerplate code for my taste) you could use CoreData and not worry about the serialization process at all--CoreData manages it all for you semi-automagically.