Problems reading a local text file with [NSString stringWithContentsOfFile & hellip;] with Objective-c returns null

This does not seem so complex. I just want to create a string object with the contents of a local text file called "test.txt" which I have placed in the root of my project.

I am trying to load it w/ the following code.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSString *textData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

//The below works, but not my above... not sure why.
//textData = @"TEST TEST";

NSLog(@"Some text from a file of %@", textData);
NSLog(@"The length of the string is %lu", [textData length]);

The output from this code is "Some text from a file of (null)" "The length of the string is 0"

I am not sure why this is returning null instead of the contents of my text file. The encoding for the file seems appropriate. Also, the file exists. Am I dragging the file to the wrong location? Do I have to add it to my NSBundle mainBundle in some way?


If you actually fill in the error parameter in +stringWithContentsOfFile:encoding:error: then it would tell you why it's returning nil. You really ought to do that. The likely reason is there is no file at that path (or you don't have permissions to read it).