文件操作在设备上不起作用
是什么导致在模拟器上运行良好的文件操作在 iOS设备 上无法运行?
当使用[NSBundle mainBundle]
时,并且文件是由FileManager
找到的,相邻文件操作有不同结果的不同原因可能是什么?
我有时会注意到这一点,只是想知道发生这种情况时应该考虑什么。
What could make a file operation that is working well on the simulator, to not be working on an iOS device?
When using [NSBundle mainBundle]
, and the file is found by FileManager
, what could be the different reasons for adjacent file operations to have different outcome?
I am noticing this sometimes, and just want to get an idea of what to think about when this happens.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎您正在尝试读取应用程序包中的文件。您可以通过代码获取其路径:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"ext"];
//然后你可以使用NSFileManager来读取/复制它
应用程序包中的所有文件都是只读的。您可以从这里获取更多信息:
http://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW8
Seems like you are trying to read a file in your application bundle. You may get its path by code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"ext"];
//Then you can use NSFileManager to read/copy it
All files in application bundle are readonly. You may get more information from here:
http://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW8
正如 @Aadhira 在上面的评论中所说,模拟器将其文件存储在 Mac 上的一堆文件夹中,而不是某种模拟的主包/文档目录沙箱中。
为了从主包中获取静态文件,您必须创建一个从
[NSBundle mainBundle]
开始的路径,并向其添加路径组件。As @Aadhira said in the comment above, the simulator stores its files inside a bunch of folders on your mac, not in some sort of simulated main bundle/docs directory sandbox.
In order to get a static file from your main bundle you must create a path starting from
[NSBundle mainBundle]
and add path components onto it.