Ios iPhone:获取资源文件夹子文件夹中的文件路径

Ios iPhone:获取资源文件夹子文件夹中的文件路径,ios,cocoa-touch,file,path,nsbundle,Ios,Cocoa Touch,File,Path,Nsbundle,我不熟悉iPhone编程。我想读取位于资源文件夹子文件夹中的文本文件的内容 资源文件夹结构如下所示: 资源 Folder1-->Data.txt Folder2-->Data.txt Folder3-->Folder1-->Data.txt 有多个名为“Data.txt”的文件,如何访问每个文件夹中的文件?我知道如何读取文本文件,但是如果资源结构类似于上面的结构,那么如何获取路径 例如,如果我想从Folder3访问“Data.txt”文件,如何获取文件路径 请建议 NSBundle* b

我不熟悉iPhone编程。我想读取位于资源文件夹子文件夹中的文本文件的内容

资源文件夹结构如下所示:

资源

  • Folder1-->Data.txt
  • Folder2-->Data.txt
  • Folder3-->Folder1-->Data.txt
  • 有多个名为“Data.txt”的文件,如何访问每个文件夹中的文件?我知道如何读取文本文件,但是如果资源结构类似于上面的结构,那么如何获取路径

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

    请建议

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

    这将为您提供捆绑包的路径。从那以后,你可以浏览你的文件夹结构。

    要继续Psychiks答案,完整的示例如下:

    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
    
    }
    
    NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pin_images/1/2.jpg"];
    UIImage * image = [UIImage imageWithContentsOfFile:filePath];
    
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg" inDirectory:@"pin_images/1/"];
    
    请注意,您可以使用-pathForResource:ofType:inDirectory访问子目录中的资源。

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

    将文件内容作为字符串加载时,如果要保留字符串,请使用自动删除的字符串的
    stringWithContentsOfFile:encoding:error:
    方法

    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
    }
    
    这与Shirkrin之前给出的答案几乎相同,但在目标上的效果略有不同。这是因为,
    initWithContentsOfFile:
    在Mac OS X上不受欢迎,在所有iPhone操作系统上都不可用。

    及以上版本都很有用,我设法使用
    pathForResource:ofType:inDirectory:
    访问我的应用程序包中不同文件夹中同名的文件

    我还找到了一个更适合我的需求的替代解决方案,所以我想与大家分享。具体请参见

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

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

    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
    
    }
    
    NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pin_images/1/2.jpg"];
    UIImage * image = [UIImage imageWithContentsOfFile:filePath];
    
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg" inDirectory:@"pin_images/1/"];
    
    作为旁注,与类型:inDirectory:等效的
    pathForResource:ofType:inDirectory:
    如下所示:

    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
    
    }
    
    NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pin_images/1/2.jpg"];
    UIImage * image = [UIImage imageWithContentsOfFile:filePath];
    
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg" inDirectory:@"pin_images/1/"];
    

    但不同文件夹中有多个同名文件夹。那么在这种情况下,如何才能实现path@Rupesh:对于第二个文件夹,您需要使用:
    [thisBundle pathForResource:@“txt”类型的“数据”目录:@“Folder3/Folder1”]
    。请注意,
    inDirectory:
    参数是相对于束根的。您应该真正使用
    [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL]
    ,这样内存就被“管理”,更重要的是,自从Mac OS X 10.4以来,
    initWithContentsOfFile:
    被弃用,并且在iPhone操作系统上不可用。所以代码只能在模拟器中工作。@Peylow-我同意你的看法。该示例是NSBundle文档中的一个修改版本,它不使用自动弹性池。@PeyloW-虽然initWithContentsOfURL确实不受欢迎,但Apple这样做只是为了要求您指定一种编码(对他们来说是明智之举)。因此,方法initWithContentsOfURL:encoding:error:在iOS 2.0及更高版本(至少通过iOS 5.1)中可用。