在 Xcode 项目文件夹中查找 plist 的路径
我的 Xcode 项目的资源文件夹中有一个 plist,但在物理驱动器上它位于项目的顶部文件夹中。我想从同一个文件中读取和写入。使用 NSBundle 可以正确读取它并且没有任何问题。我正在使用此代码来读取文件路径:
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"plist"];
但我认为 NSBundle 不允许写回,因此当我尝试写回文件时它不起作用。使用以下代码使用应用程序路径创建/更新文件,
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);
NSString * docsDirectory = [pathArray objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingPathComponent:@"MyFile.plist"];
但现在,我需要在内部文件上写入。有什么解决方案可以访问它吗?此外,我尝试提供从工作目录到项目目录的路径。它也不起作用。
I have a plist in my Xcode project's resource folder but on physical drive it's in the top folder of my project. I want to read and write from this same file. Using NSBundle reads it properly and gives no problem. I'm using this code to read file path:
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"plist"];
but i think NSBundle doesn't allow writing back and so it didn't work when i tried to write back the file. Using the following code creates/ updates file using application path
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);
NSString * docsDirectory = [pathArray objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingPathComponent:@"MyFile.plist"];
but for now, I need to write on internal file. Any solution to access it? Besides, I've tried giving path from my working directory to project directory. It also doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是 NSBundle 不支持写入,而是 iOS 本身不支持写入。 iOS 不会让你的应用程序写入自己的包。
It's not
NSBundle
that doesn't support writing, it's iOS itself. iOS will not let your app write to its own bundle.