以下是我的代码,我需要检查文档目录中是否存在文件,但是以下 API 在任何情况下都不会返回 true,而是在文档目录中创建文件。请检查我做错了什么。我已经搜索过这个,其他人说这个 API“fileExistsAtPath”应该可以工作,但它在我的情况下不起作用。请帮帮我。
NSString *recordFile=[NSString stringWithFormat:@"MyFile.acc",data.uid];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentDr = [documentPaths objectAtIndex:0]; // WHY 0 ?
NSString *uniquePath = [documentDr stringByAppendingPathComponent:recordFile];
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
NSLog(@"File found")
}
else {
NSLog(@"No File to play");
}
谢谢, 索尼
最佳答案
可能你的路径不正确。您要查找的文件的名称应该是什么?如果您有 @"MyFile1.acc"
、@"MyFile2.acc"
等(其中 1、2 是 data.uid
值)然后尝试使用这个:
NSString *recordFile=[NSString stringWithFormat:@"MyFile%i.acc",data.uid];
关于iphone - fileExistsAtPath API 始终返回 NO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6575446/