使用 [NSBundle mainBundle] pathForResource: ofType:inDirectory: 访问文件:

发布于 2024-11-14 09:09:55 字数 428 浏览 4 评论 0原文

我在名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

难如初 2024-11-21 09:09:55

我也遇到了同样的问题。
我找到的解决方案是(在 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. :)

桜花祭 2024-11-21 09:09:55

试试这个,它对我有用。

NSString *str = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"paylines.txt"];  

模拟器用

NSURL *rtfUrl = [[NSBundle mainBundle] URLForResource:@"paylines" withExtension:@".txt"];

Try this, it's working for me.

NSString *str = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"paylines.txt"];  

For simulator

NSURL *rtfUrl = [[NSBundle mainBundle] URLForResource:@"paylines" withExtension:@".txt"];
晒暮凉 2024-11-21 09:09:55

好吧,我发现了我犯的错误,我向项目添加了一个组,而不是添加更多的真实目录说明

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

咋地 2024-11-21 09:09:55

当我拖入文件时,“添加到目标”框默认情况下似乎未选中。如果我不勾选它,那么我就会遇到所描述的问题。通过删除文件然后将它们拖回来修复它,但请确保勾选“添加到目标”。
在此处输入图像描述

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".
enter image description here

北城挽邺 2024-11-21 09:09:55

您所需要做的就是确保选中下面显示的复选框。

输入图片此处描述

All you need to do is make sure that the checkbox shown below is checked.

enter image description here

此生挚爱伱 2024-11-21 09:09:55

您是否尝试在 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?

新雨望断虹 2024-11-21 09:09:55

转到“目标”-> “构建阶段”,选择您的目标,选择“构建阶段”选项卡,单击“添加构建阶段”,然后选择“添加副本文件”。将目的地更改为“产品目录”。将文件拖到“添加文件”部分。

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.

沫尐诺 2024-11-21 09:09:55

如果是 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.

层林尽染 2024-11-21 09:09:55

在遵循 @Neelam Verma 的答案或 @dawid 的答案后,其最终结果与 @Neelam Verma 的答案相同,区别在于 @dawid 的答案以将文件拖放到 Xcode 项目中开始,而 @Neelam Verma 的答案以文件已经是 Xcode 项目的一部分,我仍然无法获取 NSBundle.mainBundle().pathForResource("file-title", ofType:"type") 来查找我的视频文件。

我想也许是因为我的文件位于 Xcode 项目中嵌套的组中,这就是原因,所以我将视频文件移动到我的 Xcode 项目的根目录,仍然没有运气,这是我的代码:

 guard let path = NSBundle.mainBundle().pathForResource("testVid1", ofType:"mp4") else {
        print("Invalid video path")
        return
    }

最初,这是我的文件名: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:

 guard let path = NSBundle.mainBundle().pathForResource("testVid1", ofType:"mp4") else {
        print("Invalid video path")
        return
    }

Originally, this was the name of my file: testVid1.MP4, renaming the video file to testVid1.mp4 fixed my issue, so, at least the ofType string argument is case sensitive.

空名 2024-11-21 09:09:55

选择项目/特定文件夹-->右键单击-->将文件添加到项目 -->选择您要添加的文件。

这对我有用。

Select Project / Specific Folder --> Right Click --> Add Files to Project -- > Select the File you want to add.

This worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文