ios 如何访问项目中的本地文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7776536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access local files within project
提问by chizzle
I want to add some static files (image, binary, etc...) to my app. I've placed them under a folder named Resources
and have added it to my XCode project.
我想向我的应用程序添加一些静态文件(图像、二进制文件等)。我将它们放在一个名为的文件夹下Resources
,并将其添加到我的 XCode 项目中。
Next, I have those files added to the Copy Bundle Resources
in the Build Phases
tab in the project settings.
接下来,我将这些文件添加到项目设置Copy Bundle Resources
的Build Phases
选项卡中。
However, I can't seem to refer to them in the right way.
但是,我似乎无法以正确的方式提及它们。
For instance, reading a binary file:
例如,读取一个二进制文件:
NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:@"data.bin"];
if(![is hasBytesAvailable]) NSLog(@"wrong");
This always fails & prints wrong
这总是失败并打印 wrong
I tried a similar approach with NSData initWithContentsOfFile:
but this wouldn't even execute. Another attempt was to use [NSBundle mainBundle]
but this also didn't execute fully.
我尝试了类似的方法,NSData initWithContentsOfFile:
但这甚至无法执行。另一种尝试是使用,[NSBundle mainBundle]
但这也没有完全执行。
I'm new to iOS please help I can access my local files! Any help would be appreciated.
我是 iOS 新手,请帮助我可以访问我的本地文件!任何帮助,将不胜感激。
Thank you,
谢谢,
回答by Jim Hayes
You need to ensure that you're accessing the file in the right place. Xcode places your resource files in the application's "application bundle" in the Resourcesdirectory. There are many ways to dig 'em out.
您需要确保在正确的位置访问文件。Xcode 将您的资源文件放在Resources目录中应用程序的“应用程序包”中。有很多方法可以把它们挖出来。
A common way is to get the pathname of a file in your bundle:
一种常见的方法是获取包中文件的路径名:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *myFile = [mainBundle pathForResource: @"data" ofType: @"bin"];
If you have other files in nonstandard places in your bundle, there are variations to pathForResourcethat let your specify a directory name.
如果您的包中的非标准位置有其他文件,则pathForResource有一些变体,可以让您指定目录名称。
If you want to see the actual location on your hard-drive that the simulator is using so you can inspect it, you can say:
如果您想查看模拟器正在使用的硬盘驱动器上的实际位置以便检查它,您可以说:
NSLog(@"Main bundle path: %@", mainBundle);
NSLog(@"myFile path: %@", myFile);
Search for the "Bundle Programming Guide" in the Xcode documentation library to get started. :-)
在 Xcode 文档库中搜索“Bundle Programming Guide”以开始使用。:-)
回答by nacho4d
Use NSBundle
class for that. For Example:
NSBundle
为此使用类。例如:
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"bin"];
NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:path];
...
回答by RoyGal
Indeed calling [is open] is needed even though you "initialiseWithFileAtPath". I would think the initialisation would automatically open the file for you, but it doesn't.
即使您“initialiseWithFileAtPath”,也确实需要调用 [is open] 。我认为初始化会自动为您打开文件,但事实并非如此。