ios 无法保存plist
我可以在模拟器中保存 plist 文件,但无法在设备中保存 plist 文件。任何建议。
我用来
NSString* dictPath = [[NSBundle mainBundle] pathForResource:@"Dictionary" ofType:@"plist"];
NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:dictPath];
读取文件
并
[dict writeToFile:dictPath atomically: YES];
写入文件。
I m able to save the plist file in Simulator but I m not able to save the Plist file in the device. Any suggestion.
I m using
NSString* dictPath = [[NSBundle mainBundle] pathForResource:@"Dictionary" ofType:@"plist"];
NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:dictPath];
to read the files
and
[dict writeToFile:dictPath atomically: YES];
to write to file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,您可以根据文件将它们存储在 ~/Documents 或 ~/Library 中。问题 什么是文档目录 (NSDocumentDirectory)? 包括文档链接和您需要理解这一点的示例代码。
Generally you would store these in ~/Documents or in ~/Library depending on the file. The question What is the documents directory (NSDocumentDirectory)? includes the documentation links and sample code you need to understand this.
您需要将plist保存在Documents目录中
才能写入
读取
You need to save the plist in Documents directory
to write
to read
您无法写入主包。您只能从主包中读取。如果您想编写文件,则需要将其放入应用程序的文档目录中。
如果您需要主包中的 plist,您可以先将其复制到文档目录中,然后对其进行修改。建议进行检查以确保仅复制一次。
You can not write in to main bundle. You only can read from main bundle. If you want to write an file you need to place it in to the documents directory of your app.
If you need the plist from the main bundle you can copy it first in to the documents directory then modify it. It is advised to have a check to ensure it is copied only once.