0

我有这个树状:

Project Folder
      build
      Project
      ProjectTests
      Pictures

在子文件夹“项目”中,我有 AppDelegate 和一个名为“Alert.txt”的文件。我想从我的 AppDelegate 访问该文本文件,所以我尝试了:

- (void) applicationDidFinishLaunching:(UIApplication *)application
{
    NSLog(@"test");
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath: @"Alert.txt" ] == YES)
        NSLog(@"good");

}

当然它打印“测试”,但不是“好”。什么是正确的路径?

4

2 回答 2

3

要获取文件的路径,请使用

NSString * myFile = [[NSBundle mainBundle]pathForResource:@"Alert" ofType:@"txt"];    
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: myFile ])
    NSLog(@"good");
于 2012-06-12T14:36:28.547 回答
1

尝试以下操作:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Alert" ofType:@"txt];

于 2012-06-12T14:38:40.870 回答