以下是一个Objective-C函数,用于删除指定目录下的所有子目录:

- (void)deleteSubdirectoriesAtPath:(NSString *)directoryPath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *subdirectoryArray = [fileManager contentsOfDirectoryAtPath:directoryPath error:&error];
    if (error) {
        NSLog(@"Error: %@", error.localizedDescription);
        return;
    }
    for (NSString *subdirectory in subdirectoryArray) {
        NSString *subdirectoryPath = [directoryPath stringByAppendingPathComponent:subdirectory];
        BOOL isDirectory;
        if ([fileManager fileExistsAtPath:subdirectoryPath isDirectory:&isDirectory] && isDirectory) {
            if (![fileManager removeItemAtPath:subdirectoryPath error:&error]) {
                NSLog(@"Error: %@", error.localizedDescription);
            }
        }
    }
}

使用方法:

[self deleteSubdirectoriesAtPath:@"/path/to/parent/directory"];

其中,/path/to/parent/directory表示要删除子目录的父目录路径。该函数使用NSFileManager类获取指定目录下的所有子目录,然后逐个删除。如果删除失败,将打印错误信息。

object-c-删除子文件夹写一个函数

原文地址: https://www.cveoy.top/t/topic/rgx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录