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

标签 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 存在却没有。

可能是什么问题?

最佳答案

您正在枚举 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/

相关文章:

c++ - 网络应用程序可以在模拟器中运行,但不能在 iPhone 上运行

Iphone UIView 切换 : Previous view's subviews polluting newly switched-to view

iphone - 如何使用 UITouch 移动铅笔图像?

objective-c - NSViewController 和绑定(bind)

iphone - writeToFile路径?

ios - 如何使用 Swift 实时对视频应用滤镜

ios - 无法在 iPhone 6 上打开 Apple Pay 表

ios - 表格 View 隐藏搜索栏直到滚动

iphone - 不能用香蕉代替茶壶

iOS 9 通用链接不起作用