NSURL* suppurl = [manager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSString *path = [suppurl path];
NSLog(@" Path %@",path);
NSString* myfolder = [path stringByAppendingPathComponent:@"MyFolder"];
NSDirectoryEnumerator* myFolderDir = [manager enumeratorAtPath:myfolder];
for (NSString* file in myFolderDir)
{
NSLog(@" file %@",file);
NSString *getImagePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",file]];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:getImagePath];
NSLog(@" image path %@",getImagePath);
if (exists)
{
NSLog(@" exists");
}
第一个 NSLog(@"file %@",file);
成功记录文件名...但 BOOL 存在却没有。
可能是什么问题?
最佳答案
您正在枚举 myFolderDir
中的文件,但随后您尝试验证 path
中是否存在同名文件。替换为:
NSString *getImagePath = [myFolderDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",file]];
另外,请注意,您不需要这样做:
[NSString stringWithFormat:@"%@",file]
.. file
已经是 NSString
。
如果您尝试枚举该文件夹中的图像,请考虑测试文件扩展名:
if ([[file pathExtension] isEqualToString: @"jpg"]) {
// load the image
[self loadImage: [myFolderDir stringByAppendingPathComponent:file]];
}
关于iphone - 从应用程序目录中检索文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9412685/