Cocoa - Read content of a file?
I'm just trying to read the content of a text file that is in my project in the main folder.
// file name is "logs"
// it returns nil
NSString *bundlePath = [[N开发者_JAVA技巧SBundle mainBundle] pathForResource:@"logs" ofType:nil];
// file name is "logs.txt"
// it returns nil
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"logs" ofType:@"txt"];
Questions:
- How can I get the path to a file that is included in my project?
- How can I get the path to the root folder of my project?
The fact that a file is present in your project directory doesn’t mean the file will be copied to the corresponding target application bundle. You need to add the file to the target via the Copy Bundle Resources build phase.
In order to inspect and edit the list of files that are copied to the bundle directory in Xcode 4:
- Click your root project in Project Navigator
- Click your target
- Select Build Phases > Copy Bundle Resources
Note: when you add a file to the project via Xcode, the add file dialog gives you a choice of adding that file to a target so that you don’t need to do these steps later.
精彩评论