如何向plist添加新项目? [英] How do I append a new item to a plist?

查看:140
本文介绍了如何向plist添加新项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,我有两个plist文件来存储主题。一个是包含默认主题的只读文件,一个包含用户创建的自定义主题。我使用plist文件,因为它很容易从plist读取并创建新的Theme对象。

In my iPhone app, I have two plist files to store "Themes". One is a read-only file containing default themes, and one contains custom Themes that the user has created. I'm using plist files because it's very easy for me to read from the plist and create new Theme objects.

我的plist是一个字典对象数组。

My plist is an array of dictionary objects.

有没有任何简单的方法来附加一个新的字典对象到我的plist文件?或者我需要将文件读入内存,附加新的字典对象,并将其写回文件系统?

Is there any easy way to append a new dictionary object to my plist file? Or do I need to read the file into memory, append the new dictionary object, and write it back to the filesystem?

谢谢!

推荐答案

使用Cocoa,您需要将该文件读入内存,附加新的字典对象,并将其写回文件系统。如果你使用XML plist,你可以很容易地解析它,并逐渐写入文件,但它也会有点大,所以它不太可能是值得的。

With Cocoa, you need to read the file into memory, append the new dictionary object, and write it back to the filesystem. If you use an XML plist, you could pretty easily parse it and incrementally write to the file, but it'd also be quite a bit bigger, so it's unlikely to be worth it.

如果重写plist花费的时间太长,您应该使用数据库(或者通过Core Data)进行调查。除非文件是巨大的,我怀疑这将是一个问题,即使有iPhone的内存容量和闪存写入速度。

If rewriting the plist is taking too long, you should investigate using a database instead (perhaps via Core Data). Unless the file is huge, I doubt this will be an issue even with the iPhone's memory capacity and flash write speed.

这篇关于如何向plist添加新项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆