iphone - 如何删除应用程序指定目录中的所有文件?

给定一个目录[[self documentsDirectory] ​​stringByAppendingPathComponent:@"Photos/"]我如何删除这个文件夹中的所有文件?

(假设一个正确的文档目录路径)

最佳答案

NSFileManager *fm = [NSFileManager defaultManager];
NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"];
NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:directory error:&error]) {
    BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@%@", directory, file] error:&error];
    if (!success || error) {
        // it failed.
    }
}

如果存在错误,我会让你做一些有用的事情。

https://stackoverflow.com/questions/2226600/

相关文章:

objective-c - 分组的 UITableView 在底部有 20px 的额外填充

objective-c - iOS:如何将 UIViewAnimationCurve 转换为 UIV

ios - 使用 Storyboard 的条件转场

ios - MKMapView MKPointAnnotation 点击​​事件

ios - 如何从 CGPoint 和 CGSize 创建 CGRect?

ios - 使用自动布局,如何根据图像使 UIImageView 的大小动态变化?

objective-c - Interface Builder 中的 IBOutletCollect

objective-c - 奇怪的错误 NSAssert

objective-c - 有没有一种简单的方法可以将 ISO8601 时间戳转换为格式化的 NSD

ios - 字符串的静态 NSArray - 如何/在哪里初始化 View Controller