objective-c - 如何使用NSFileManager setAttributes递归更改目录的权限

标签 objective-c

我目前正在使用NSFileManager setAttributes更改目录的权限。我的问题是它似乎没有递归地这样做。有什么办法强迫它这样做?

最佳答案

我认为没有内置的方法可以做到这一点,但是要做类似的事情并不难:

NSString *path = @"/The/root/directory";
NSDictionary *attributes;   // Assume that this is already setup


NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *subPaths = [fileManager subpathsAtPath:path];
for (NSString *aPath in subPaths) {
    BOOL isDirectory;
    [fileManager fileExistsAtPath:aPath isDirectory:&isDirectory];
    if (isDirectory) {
        // Change the permissions on the directory here
        NSError *error = nil;
        [fileManager setAttributes:attributes ofItemAtPath:aPath error:&error];
        if (error) {
            // Handle the error
        }
    }
}


这未经测试,但应该为您提供一个起点。

关于objective-c - 如何使用NSFileManager setAttributes递归更改目录的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55614549/

相关文章:

objective-c - 发送消息到已释放实例时,-dealloc崩溃

objective-c - 用户更新时 NSUserDefaults 是否重置

ios - 为什么我的测试在 Parse 中不起作用?

ios - 匿名用户登录成功后持久化在数据库中

ios - 在 objective-c 中强制旋转

iphone - 应用程序处于后台状态时的 CLLocationManager

ios - 我无法在 MagicalRecord 中找到 FirstByAttribute

ios - UIImage imageNamed内存泄漏

ios - 使用其他三个 View Controller 设置 UITabBarController

iphone - iPhone 应用程序设计建议