Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 346
Henrik Hillestad Løvold Avatar asked Jan 21 '26 03:01

Henrik Hillestad Løvold


1 Answers

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"];
like image 157
Martin R Avatar answered Jan 23 '26 18:01

Martin R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!