pList 还是硬编码对象?

发布于 2024-12-05 17:39:35 字数 364 浏览 0 评论 0原文

在我的应用程序中,我需要 Person 类的对象的简短列表。每个Person都有一些属性,例如namefirstNameage等。 到目前为止,所有对象都用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思慕 2024-12-12 17:39:35

使用 plist 文件的好处如下:

  • 您的项目中可以有多个 plist,并可以通过代码轻松切换到您想要加载的 plist。例如,当使用多个数据池测试您的应用程序时,或者例如为每种本地化提供一个 plist(一个用于英语,一个用于西班牙语,一个用于中文,等等)时,
  • 您可以加载/卸载数据,因此 很有用它们不会卡在内存中
  • 您可以保存 plist 文件,以任何原因修改它,然后恢复它,而不会丢失您修改过的“真实”应用程序代码。

但是...如果您的硬编码非常干净,将静态数据存储到自定义类中,及其访问器等...所有这些都将适用于您的自定义类(它可以保存为单独的文件,加载进入内存,然后释放,本地化,...),所以 plist 文件不会有任何明显的好处。

The benefits of using a plist file are the following :

  • you can have many plist in your project and easily switch by code to the plist you want to load. Usefull for example when testing your app with multiple pools of datas, or for example to have one plist for each localization (one for english, one for spanish, one for chinese, etc...)
  • You can load/unload the datas, so they don't get stuck into memory
  • You can save a plist file, modify it for any reason, and restore it, without loosing the "real" app code you did modify.

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.

时光与爱终年不遇 2024-12-12 17:39:35

我自己也有类似的情况,最后还是选择了 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?).

苏辞 2024-12-12 17:39:35

属性列表也是对象图,只不过这些对象是通用容器而不是项目域的模型。因此它们并不更小或更容易使用。磁盘中 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文