使用 [NSBundle mainBundle] pathForResource: ofType:inDirectory: 访问文件:
我在名为 TextFiles
的文件夹中添加了一个文件 paylines.txt
,该文件夹位于 Xcode 中我的 iOS 项目的 Resources
文件夹内。
这是我用来访问该文件的代码:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"paylines" ofType:@"txt" inDirectory:@"TextFiles"];
NSLog(@"\n\nthe string %@",filePath);
代码打印:
2011-06-07 14:47:16.251 slots2[708:207]
the string (null)
I have a file paylines.txt
added inside the folder named TextFiles
which resides inside the Resources
folder of my iOS project in Xcode.
This is the code I use to access the file:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"paylines" ofType:@"txt" inDirectory:@"TextFiles"];
NSLog(@"\n\nthe string %@",filePath);
The code prints:
2011-06-07 14:47:16.251 slots2[708:207]
the string (null)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我也遇到了同样的问题。
我找到的解决方案是(在 xcode 4.x 中):
Go to : Target -> “构建阶段”-> “复制捆绑资源”
然后在此处添加该特定文件。
如果该文件已添加,请将其删除并重新添加。
清理项目并运行。它有效。:)
I was also having the same problem.
The Solution i found is ( in xcode 4.x):
Go to : Target -> "Build Phases" -> "copy bundle Resources"
Then add that particular file here.
If that file is already added , delete it and add it again.
clean the project and RUN. It works. :)
试试这个,它对我有用。
模拟器用
Try this, it's working for me.
For simulator
好吧,我发现了我犯的错误,我向项目添加了一个组,而不是添加更多的真实目录说明
well i found out the mistake i was committing i was adding a group to the project instead of adding real directory for more instructions
当我拖入文件时,“添加到目标”框默认情况下似乎未选中。如果我不勾选它,那么我就会遇到所描述的问题。通过删除文件然后将它们拖回来修复它,但请确保勾选“添加到目标”。
When I drag files in, the "Add to targets" box seems to be un-ticked by default. If I leave it un-ticked then I have the problem described. Fix it by deleting the files then dragging them back in but making sure to tick "Add to targets".
您所需要做的就是确保选中下面显示的复选框。
All you need to do is make sure that the checkbox shown below is checked.
您是否尝试在 XCTest 内执行此操作并处于砸毁笔记本电脑的边缘?这是适合您的线程:为什么不能在单元内编码测试找到捆绑资源吗?
Are you attempting to do this inside of an XCTest and on the verge of smashing your laptop? This is the thread for you: Why can't code inside unit tests find bundle resources?
转到“目标”-> “构建阶段”,选择您的目标,选择“构建阶段”选项卡,单击“添加构建阶段”,然后选择“添加副本文件”。将目的地更改为“产品目录”。将文件拖到“添加文件”部分。
Go to "Target" -> "Build Phases", select your target, select the “Build Phases” tab, click “Add Build Phase”, and select “Add Copy Files”. Change the destination to “Products Directory”. Drag your file into the “Add files” section.
如果是 Mac OSX,
请转到目标 ->构建阶段
单击 + 复制新文件构建阶段
选择产品目录并将文件拖放到那里。
清理并运行项目。
In case of Mac OSX,
Go to Targets -> Build Phases
click + to Copy new files build phases
Select product directory and drop the file there.
Clean and run the project.
在遵循 @Neelam Verma 的答案或 @dawid 的答案后,其最终结果与 @Neelam Verma 的答案相同,区别在于 @dawid 的答案以将文件拖放到 Xcode 项目中开始,而 @Neelam Verma 的答案以文件已经是 Xcode 项目的一部分,我仍然无法获取 NSBundle.mainBundle().pathForResource("file-title", ofType:"type") 来查找我的视频文件。
我想也许是因为我的文件位于 Xcode 项目中嵌套的组中,这就是原因,所以我将视频文件移动到我的 Xcode 项目的根目录,仍然没有运气,这是我的代码:
最初,这是我的文件名:
testVid1.MP4
,将视频文件重命名为testVid1.mp4
解决了我的问题,因此,至少ofType
字符串参数区分大小写。After following @Neelam Verma's answer or @dawid's answer, which has the same end result as @Neelam Verma's answer, difference being that @dawid's answer starts with the drag and drop of the file into the Xcode project and @Neelam Verma's answer starts with a file already a part of the Xcode project, I still could not get
NSBundle.mainBundle().pathForResource("file-title", ofType:"type")
to find my video file.I thought maybe because I had my file was in a Group nested in the Xcode project that this was the cause, so I moved the video file to the root of my Xcode project, still no luck, this was my code:
Originally, this was the name of my file:
testVid1.MP4
, renaming the video file totestVid1.mp4
fixed my issue, so, at least theofType
string argument is case sensitive.选择项目/特定文件夹-->右键单击-->将文件添加到项目 -->选择您要添加的文件。
这对我有用。
Select Project / Specific Folder --> Right Click --> Add Files to Project -- > Select the File you want to add.
This worked for me.