Xcode,在测试期间访问“Supporting Files”文件夹中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10842286/
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
Xcode, accessing files in "Supporting Files" folder during tests
提问by lurscher
I have a project with tests on Xcode 4.3.2, i want to access some files from the tests when they are run. I added the files to the automatically generated "Supporting Files" folder, but i can't access it when the test is run, i will get errors because the file is not found.
我有一个在 Xcode 4.3.2 上进行测试的项目,我想在运行时访问测试中的一些文件。我将文件添加到自动生成的“Supporting Files”文件夹中,但是在运行测试时无法访问它,因为找不到该文件,我将收到错误消息。
I tried these combinations:
我尝试了这些组合:
myFile
myFile
Supporting Files/myFile
Supporting Files/myFile
../Supporting Files/myFile
../Supporting Files/myFile
None of these worked though. Any idea what i'm doing wrong?
然而这些都没有奏效。知道我做错了什么吗?
回答by Alladinian
Once you add them to your Supporting Files
folder, you have to check your test target (target membership) in order for them to be available in the bundle. You can also verify this (along with their location) in your target info under 'build phases'.
Then (inside your tests) you get the path of your files like this:
将它们添加到Supporting Files
文件夹后,您必须检查您的测试目标(目标成员资格),以便它们在捆绑包中可用。您还可以在“构建阶段”下的目标信息中验证这一点(以及它们的位置)。然后(在您的测试中)您将获得如下文件的路径:
NSString *filePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"index" ofType:@"html"]; //Whatever your file - extension is
I attach a screenshot as a quick reference:
我附上截图作为快速参考:
PS. The screenshot is from an OSX project but the procedure is identical for iOS
附注。屏幕截图来自 OSX 项目,但过程与 iOS 相同