pList 还是硬编码对象?
在我的应用程序中,我需要 Person 类的对象的简短列表。每个Person都有一些属性,例如name、firstName、age等。 到目前为止,所有对象都用 Objective-C 进行硬编码并添加到 NSMutableArray 中。 这种方法非常适合我的需求,因为我不需要在运行时添加任何其他对象。
不知何故,我想到使用 plist 而不是硬编码对象可能是一个更好的主意,因此我从数组创建了一个 plist。 令我惊讶的是,plist 文件并不小,现在我想知道使用硬编码对象是否是更好的方法。
我不需要任何核心数据(我猜),因为我需要的只是一个永远不会改变且用户不应修改的对象列表。
In my application I need short list of objects of class Person. Each Person has a few properties like name, firstName, age and so on.
So far all objects were hard coded in Objective-C and added to an NSMutableArray.
This approach works perfect for my needs since I don't need to add any additional objects during runtime.
Somehow I had the idea of working with a plist instead of hard coded objects may be a much better idea and so I created a plist from my array.
To my supprise the plist file was not exactly small and now I wonder if working with hard coded objects may be the better approch.
I don't need any Core Data (I guess) since all I need is a list of objects that will never change and should not be modified by the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 plist 文件的好处如下:
但是...如果您的硬编码非常干净,将静态数据存储到自定义类中,及其访问器等...所有这些都将适用于您的自定义类(它可以保存为单独的文件,加载进入内存,然后释放,本地化,...),所以 plist 文件不会有任何明显的好处。
The benefits of using a plist file are the following :
But... if your hard-coding is ultra clean, with static datas stored into a custom class, with its accessors, etc... all of this will apply to your custom class (it can be saved as an invidual file, loaded into memory then released, localized, ...), so then the plist files won't have any visible benefits.
我自己也有类似的情况,最后还是选择了 plist。它从我的应用程序启动中删除了许多行重复代码(这对我来说是最大的好处),并使以后添加或更改项目变得更加容易。如果逐个字节地在 plist 中与代码相比需要更多的“字符”(显然编译时代码更小,但我们在谈论什么,几千字节?),我会感到惊讶。
I had a similar situation myself and went with the plist in the end. It took many lines of repetetive code out of my app startup (which was the biggest benefit for me) and made adding or changing items later on much easier. I'd be surprised if, byte-for-byte, it took significantly more "characters" to do it in a plist versus code (obviously the code is smaller when compiled, but what are we talking about, a few kilobytes?).
属性列表也是对象图,只不过这些对象是通用容器而不是项目域的模型。因此它们并不更小或更容易使用。磁盘中 plist 的大小并不重要,因为您只需反序列化一次,这几乎是应用程序生命周期的 0%,而且它也可以忽略不计,因为您没有处理大量数据。
如果您尝试使用 plist 来实现更大的灵活性(也许用于单元测试),您可以实现 NSCoding,并将归档/取消归档到二进制 plist。
A property list is also a graph of objects, except those objects are generic containers and not a model of your project domain. So they are not smaller or easier to work with. The size of the plist in disk is not important because you only have to deserialize it once, which will be nearly 0% of your application lifetime, and it's also negligible because you are not dealing with a big amount of data.
If you are trying to use a plist to achieve more flexibility (maybe for unit testing), you can implement NSCoding, and archive/unarchive to binary plist.