ios iPhone:获取资源文件夹子文件夹内的文件路径

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

iPhone : Get the file path which is within subfolder of Resource folder

ioscocoa-touchfilepathnsbundle

提问by Rupesh

I am new to iPhone programming. I want to read the content of a text file located in a subfolder of the Resource folder.

我是 iPhone 编程的新手。我想读取位于 Resource 文件夹子文件夹中的文本文件的内容。

The Resource folder structure is the following:

资源文件夹结构如下:

Resource

资源

  1. Folder1---->Data.txt
  2. Folder2---->Data.txt
  3. Folder3---->Folder1---->Data.txt
  1. Folder1---->Data.txt
  2. Folder2---->Data.txt
  3. Folder3---->Folder1---->Data.txt

There are multiple files named "Data.txt", so how can I access the files in each folder? I know how to read the text file, but if the Resource structure is similar to the above structure then how can I get the path?

有多个名为“Data.txt”的文件,那么如何访问每个文件夹中的文件?我知道如何读取文本文件,但是如果 Resource 结构与上述结构相似,那么我如何获取路径?

For example, if I want to access the "Data.txt" file from Folder3, how can I get the file path?

例如,如果我想访问 Folder3 中的“Data.txt”文件,我如何获取文件路径?

Please suggest.

请建议。

回答by PeyloW

Your "resource folder"is actually the contents of your main bundle, also know as the application bundle. You use pathForResource:ofType:or pathForResource:ofType:inDirectory:to get the full path for a resource.

您的“资源文件夹”实际上是主包的内容,也称为应用程序包。您可以使用pathForResource:ofType:pathForResource:ofType:inDirectory:来获取资源的完整路径。

Loading the contents of a file as a string is done with the stringWithContentsOfFile:encoding:error:method for an autoreleased string of with initWithContentsOfFile:encoding:error:if you want a retained string.

如果您想要保留字符串stringWithContentsOfFile:encoding:error:initWithContentsOfFile:encoding:error:则可以使用 with 的自动释放字符串的方法将文件内容作为字符串加载。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" 
                                                     ofType:@"txt"
                                                inDirectory:@"Folder1"];
if (filePath != nil) {
  theContents = [NSString stringWithContentsOfFile:filePath
                                          encoding:NSUTF8StringEncoding
                                             error:NULL];
  // Do stuff to theContents
}

This is almost the same answer as given by Shirkrin previously, but with the slight difference that it works on target. This is because initWithContentsOfFile:is deprecated on Mac OS X, and not available at all iPhone OS.

这与 Shirkrin 之前给出的答案几乎相同,但略有不同,它适用于目标。这是因为initWithContentsOfFile:在 Mac OS X 上已被弃用,并且在所有 iPhone 操作系统上都不可用。

回答by Shirkrin

To continue psychotiks answer a full example would look like this:

要继续 Psychotiks 回答一个完整的例子看起来像这样:

NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *filePath = nil;

if (filePath = [thisBundle pathForResource:@"Data" ofType:@"txt" inDirectory:@"Folder1"])  {

    theContents = [[NSString alloc] initWithContentsOfFile:filePath];

    // when completed, it is the developer's responsibility to release theContents

}

Notice that you can use -pathForResource:ofType:inDirectory to access ressources in sub directories.

请注意,您可以使用 -pathForResource:ofType:inDirectory 来访问子目录中的资源。

回答by Steph Sharp

Shirkrin's answerand PeyloW's answerabove were both useful, and I managed to use pathForResource:ofType:inDirectory:to access files with the same name in different folders in my app bundle.

Shirkrin 的回答PeyloW 的上述回答都很有用,我设法pathForResource:ofType:inDirectory:用来访问我的应用程序包中不同文件夹中的同名文件。

I also found an alternative solution herethat suited my requirements slightly better, so I thought I'd share it. In particular, see this link.

我还在这里找到了一个更适合我的要求的替代解决方案,所以我想我会分享它。特别是,请参阅此链接

For example, say I have the following Folder References (blue icons, Groups are yellow):

例如,假设我有以下文件夹引用(蓝色图标,组为黄色):

enter image description here

在此处输入图片说明

Then I can access the image files like this:

然后我可以像这样访问图像文件:

NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pin_images/1/2.jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];

As a side note, the pathForResource:ofType:inDirectory:equivalent looks like this:

作为旁注,pathForResource:ofType:inDirectory:等价物如下所示:

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg" inDirectory:@"pin_images/1/"];

回答by psychotik

    NSBundle* bundle = [NSBundle mainBundle];
    NSString* path = [bundle bundlePath];

This gives you the path to your bundle. From there on, you can navigate your folder structure.

这为您提供了包的路径。从那时起,您可以浏览您的文件夹结构。