【问题标题】:Deleteing more than one file from iphone documents folder?从 iphone 文档文件夹中删除多个文件?
【发布时间】:2011-07-21 14:50:38
【问题描述】:

我的 Iphone 应用程序正在将照片保存到文件夹中。我想在使用后删除所有这些文件形式的文件夹。我知道如何使用路径一次删除一个文件

if ([fileMgr removeItemAtPath:filePath error:&error] != YES)
    NSLog(@"Unable to delete file: %@", [error localizedDescription]);

但我想一次性删除所有文件。我所有的文件都是 .png 格式我试过 *.png 但不工作。

【问题讨论】:

    标签: objective-c ios4


    【解决方案1】:
    /* dir: the directory where your files are */
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *items = [fm contentsOfDirectoryAtPath:dir error:&error];
    if (error) { /* ... */ }
    
    /* delete png files */
    for (NSString *item in items) {
        if ([[item pathExtension] isEqualToString:@"png"]) {
            NSString *path = [dir stringByAppendingPathComponent:item];
            [fm removeItemAtPath:path error:&error];
            if (error)
                { /* ... */ }
        }
    }
    

    【讨论】:

    • 感谢您的帮助,我刚刚在 if 条件之后添加了以下内容,并且它有效。 NSString *path = [documentsDirectory stringByAppendingPathComponent:item]; [fileMgr removeItemAtPath:path error:&error];
    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 2017-12-17
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多