inside an iOS tweak, I have a long text and store it in a text file at the same folder level with .xm file. Let's say I have a tweak project with a Tweak.xm file at the top level of the project, and another file called myFile.txt placed at the same folder level as Tweak.xm. How do I read the content of this txt file in the xm code?
I tried this Objective-C code but an error came up as the file path may not be correct.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"];
NSError *error = nil;
NSString *fileContent = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];
if (error) {
NSLog(@"Error reading file: %@", error);
NSLog(@"File Path: %@", filePath);
// the log error is `Error reading file: Error Domain=NSCocoaErrorDomain Code=258 "The file name is invalid."`
} else {
NSLog(@"File Content:\n%@", fileContent);
}
I did a little redesign but it should work correctly for your purposes.
Example Code:
Test run: