NSBundle pathForResource: returns nil

10,150

filePath already contains the suffix ".txt", therefore you must not specify it again when locating the resource. Either

NSString* filePath = @"filename.txt";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:nil];

or

NSString* filePath = @"filename";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"txt"];
Share:
10,150
Henrik Hillestad Løvold
Author by

Henrik Hillestad Løvold

I am a Norwegian Computer Science student. I write Java, C++ and Objective-C, and I like to develop iOS apps.

Updated on June 14, 2022

Comments

  • Henrik Hillestad Løvold
    Henrik Hillestad Løvold almost 2 years

    I am attempting to access a .txt file from my supporting files folder in Xcode on iOS using the following piece of code:

    NSString* filePath = @"filename.txt";
    
    NSLog(@"File path: %@", filePath);
    
    NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"txt"];
    
    NSLog(@"File root: %@", fileRoot);
    

    The first NSLog prints just what I expect it to print, but the last NSLog always simply prints

    File root: (null)

    Accessing text from the file after (attempting to) reading it into memory also simply gives me a (null) printout.

    The solution for this problem is probably right under my nose, I just can't seem to find it. Any help is very appreciated! Thanks in advance.