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.
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"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With