从应用程序目录中检索文档

时间:2012-02-23 12:02:13

标签: iphone objective-c ios

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不存在。

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

您正在枚举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]];
}