I am learning how to write simple Objective-C programs (without a GUI). I created a new xcode project and have the following files in the top level directory:
main.m
data.txt
How can I get the contents of data.txt as an NSString for use in main.m?
I tried:
+ (NSString *)loadTextFileToString:(NSString*)fileDest {
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileDest
ofType:@"txt"];
NSError *error = nil;
NSString *fileContent = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];
if(error) {
NSLog(@"ERROR while loading from file: %@", error);
}
return fileContent;
}
And passing @ "data" as an argument, but it gives me an error:
ERROR while loading from file: Error Domain=NSCocoaErrorDomain Code=258 "The file name is invalid."
What is the right way to do this?
source
share