Search code examples
ioscocoa-touchfilepathnsbundle

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


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

The Resource folder structure is the following:

Resource

  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?

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

Please suggest.


Solution

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

    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.