找出文件是否存在?

时间:2012-03-02 13:47:22

标签: iphone objective-c xcode nsfilemanager

我试图通过运行

来确定文档是否存在
    NSString *DocumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *theFile = [[DocumentPath stringByAppendingPathComponent:FileName] retain];
    NSLog(@"File : %@",theFile);
    NSLog(@"FileName : %@", FileName);
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:theFile];
    NSLog(@"FileExists : %@", fileExists);

我知道文档就在那里,但我想运行它来查找,如果说它已被删除,那么就做其他事情了。但我得到的只是我的NSLog

 FileExists : (null)

我看了一些教程,他们说这是对的。我错过了什么?

2 个答案:

答案 0 :(得分:1)

使用此行代替您的行:

NSLog(@"FileExists : %d", fileExists);

%@ format说明符仅用于对象,并在指定对象的描述方法调用结果中打印

答案 1 :(得分:1)

此代码适合我。在Resources->file.txt

中保存文件
 NSString *myFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file.txt"];
 NSLog(@"FileExist %d",[[NSFileManager defaultManager] fileExistsAtPath:myFilePath]);
相关问题