Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Text File from Xcode Bundle

Why can't I read the content of foo.rtf? I've already put it in Xcode bundle. fileRoot still contains null.

NSString* filePath = @"foo";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"rtf"];       

NSLog(@"File root contains %@", fileRoot);

@TheAmateurProgrammer, Thank you for the diagram. I just added foo.rtf into the bundle resources. The result for fileRoot is still null. What step am I still missing?

Target's "Copy Bundle Resources" in Build Phases now contains foo.rtf. (I can't insert picture as I'm still a newbie).

(I will add the content reading after I can get fileRoot to point correctly).

like image 267
zakton Avatar asked Feb 19 '23 18:02

zakton


1 Answers

You added the file, but is it really copied to your application bundle?

Make sure your rtf file is copied into your resource.

Secondly, you're only getting the path of the rtf, and not the contents of the rtf.

NSString *fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"rtf"]; 
NSString *contents = [[[NSAttributedString alloc] initWithRTF:[NSData dataWithContentsOfFile:fileRoot] documentAttributes:NULL] string];

That will get the contents of the rtf.

like image 119
TheAmateurProgrammer Avatar answered Feb 26 '23 23:02

TheAmateurProgrammer