NSBundle pathForResource: returns nil

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.

Upvotes: 2

Views: 10228

Answers (1)

Martin R
Martin R

Reputation: 539685

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

Upvotes: 18

Related Questions