PList 鸡/蛋场景
我想读取/写入cache.plist
如果我想读取存储在资源文件夹中的现有预制plist 文件,我可以:
path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathWithComponent@"cache.plist"];
NSMutableDictionary *root = ...
但是我希望从iPhone 读取它。
不能,Resources 文件夹是只读的。
所以我需要使用:
NSDocumentDirectory, NSUserDomain,YES
那么如何将我的 plist 文件预安装到文档目录位置?
因此,这意味着我不必在启动时使用乱七八糟的代码复制 plist 文件。 (除非这是唯一的方法)。
I want to read/write to cache.plist
If I want to read an existing premade plist file stored in the resources folder I can go:
path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathWithComponent@"cache.plist"];
NSMutableDictionary *root = ...
But then I wish to read it from the iPhone.
Can't, the Resources folder is only readable.
So I need to use:
NSDocumentDirectory, NSUserDomain,YES
So how can I have my plist file preinstalled to the Document Directory location?
Thus meaning I don't have to mess around with untidy code copying the plist file over at startup. (Unless that's the only way).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这并不是您真正想要的,但据我所知,将文档放入“文档”文件夹的唯一方法是将其实际复制到那里......但仅限于第一次启动时。我正在为 sqlite 数据库做类似的事情。代码如下,它可以工作,但请注意,它可以进行一些清理:
只需调用您的 AppDelegate - 真的不太乱吗?
I know this isn't really what you're after, but as far as I know the only way to get the document into the Documents folder IS to actually copy it there...but only on the first startup. I'm going something similar for a sqlite database. Code is below, it works but please note it could do with a little bit of cleaning up:
Just call in your AppDelegate - not too messy really?
简单的。首先查看它是否在文档目录中。如果不是,请在应用程序的资源文件夹中找到它(
[[NSBundle mainBundle] pathForResource...]
),然后使用 < 将其复制到文档目录中a href="http://developer.apple.com/iphone/library/documentation/cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/doc/uid/20000305-SW14" rel= “nofollow noreferrer”>[[NSFileManager defaultManager] copyItemAtPath:...]
。然后使用文档目录中的新副本而不受惩罚。Easy. Look first to see if it's in the documents directory. If it's not, find it inside your app's Resources folder (
[[NSBundle mainBundle] pathForResource...]
), then copy it into the documents directory using[[NSFileManager defaultManager] copyItemAtPath:...]
. Then use the fresh copy in the documents directory with impunity.最终产品
The final product