给定目录[[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.
    }
}

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

Objective-c相关问答推荐

如果DISPATCH_SOURCE_SET_EVENT_HANDLER中的SELF需要为弱

uipageviewcontroller 类在设置多个视图控制器时崩溃

如何从 C 方法调用 Objective-C 方法?

从 float 或 double 实例化 NSDecimalNumber 的正确方法

Objective-C:在字符串中查找数字

使用 ARC,什么更好:alloc 或 autorelease 初始化程序?

如何处理 Xcode 警告没有以前的函数原型......?

iOS - 应用程序关闭时的核心位置和地理围栏

iOS 7 - viewDidLoad 和 viewDidAppear 之间的区别

如何测试生产推送通知?

检测设备是否支持电话?

如何在Objective C(NSRegularExpression)中编写正则表达式?

从 UITabBarController 在当前上下文中呈现模态视图控制器后出现黑屏

如何在 Cocoa 中获取 NSArray 的第一个 x 元素?

Objective C 中的调用方法或发送消息

在 switch 语句中使用 NSString

zoom UIView 及其所有子项

iOS 7 中的 UITextView 链接检测

状态栏文本 colored颜色 iOS 7

将自定义子视图(在 xib 中创建)添加到视图控制器的视图 - 我在做什么错