简体   繁体   English

属性列表是否应该由其自己的模型对象管理?

[英]Should a property list be managed by its own model object?

I am building an iPhone app that has some default data inside it via a property list. 我正在构建一个iPhone应用程序,该应用程序通过属性列表在其中包含一些默认数据。 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? 什么是管理此数据的最佳方法,以便用户可以从此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. 我目前正在对NSObject进行子类化,并将plist数据映射到该对象中的属性,并带有将数据读/写到该对象的方法。 I have read about the NSCoding protocol and NSCoder but am not sure how to implement this in my custom class. 我已经阅读了有关NSCoding协议和NSCoder的信息,但不确定如何在我的自定义类中实现此功能。

Any help will be appreciated. 任何帮助将不胜感激。

Seems the answer is in this link: 似乎答案在此链接中:

http://mojomonkeycoding.com/tag/nscoding/ http://mojomonkeycoding.com/tag/nscoding/

I guess you do not worry about super being initWithCoder in these cases. 我猜您在这些情况下不必担心initWithCoder是超级类。

If you really want to keep the data in a plist then you can use NSString:propertyList. 如果您确实想将数据保留在plist中,则可以使用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. 然后,您可以使用NSPropertyListSerialization将其写回。

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. 如果您感兴趣的话,有很多方法可以简单地加载和保存数据。除了NSCoder(我喜欢很多样板代码)之外,您可以使用CoreData而不用担心序列化过程- -CoreData半自动为您管理所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM