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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6263289/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:15:10  来源:igfitidea点击:

accessing a file using [NSBundle mainBundle] pathForResource: ofType:inDirectory:

objective-ciosxcodefilepathnsbundle

提问by Yadnesh

I have a file paylines.txtadded inside the folder named TextFileswhich resides inside the Resourcesfolder of my iOS project in Xcode.

paylines.txt在名为的文件夹中添加了一个文件,该文件TextFiles位于ResourcesXcode 中我的 iOS 项目文件夹内。

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)

采纳答案by Yadnesh

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

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

回答by Neelam Verma

I was also having the same problem. The Solution i found is ( in xcode 4.x):

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

清理项目并运行。有用。:)

回答by dawid

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

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

回答by Rajneesh071

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"];

回答by RawMean

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

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

enter image description here

在此处输入图片说明

回答by evanflash

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?

您是否正在尝试在 XCTest 中执行此操作并且即将砸碎您的笔记本电脑?这是您的主题:为什么单元测试中的代码不能找到捆绑资源?

回答by Brian Ogden

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.

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

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:

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

 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.mp4fixed my issue, so, at least the ofTypestring argument is case sensitive.

最初,这是我的文件名:testVid1.MP4,重命名视频文件以testVid1.mp4解决我的问题,因此,至少ofType字符串参数区分大小写。

回答by megha

In case of Mac OSX,

在 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.

清理并运行项目。

回答by Valeriy

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.

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

回答by YSR fan

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

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

This worked for me.

这对我有用。