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

Pie*_*rre 5 objective-c

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

Nic*_*rge 4

我不认为有一个内置的方法可以做到这一点,但做这样的事情应该不难:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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