NSFileManager和NSFilePosixPermissions [英] NSFileManager & NSFilePosixPermissions

查看:581
本文介绍了NSFileManager和NSFilePosixPermissions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为NSFilePosixPermissions使用八进制权限(用于chmod). 这是我现在所做的:

I want to use the octal permissions (used for chmod) for NSFilePosixPermissions. Here is what I did now:

NSFileManager *manager = [NSFileManager defaultManager];
NSDictionary *attributes;

[attributes setValue:[NSString stringWithFormat:@"%d", 0777] 
             forKey:@"NSFilePosixPermissions"]; // chmod permissions 777
[manager setAttributes:attributes ofItemAtPath:@"/Users/lucky/Desktop/script" error:nil];

我没有收到任何错误,但是当我用"ls -o"检查结果时,权限不是-rwxrwxrwx.

I get no error, but when I check the result with "ls -o" the permission are't -rwxrwxrwx.

怎么了? 感谢您的帮助.

What's wrong? Thanks for help.

推荐答案

首先,NSFilePosixPermissions是常量的名称.它的值也可能相同,但这并不能保证. NSFilePosixPermissions常量的值可以在框架版本之间更改,例如. G.从@"NSFilePosixPermissions"@"posixPermisions".这会破坏您的代码.正确的方法是将常量用作NSFilePosixPermissions,而不是@"NSFilePosixPermissions".

First, NSFilePosixPermissions is the name of a constant. Its value may also be the same, but that’s not guaranteed. The value of the NSFilePosixPermissions constant could change between framework releases, e. g. from @"NSFilePosixPermissions" to @"posixPermisions". This would break your code. The right way is to use the constant as NSFilePosixPermissions, not @"NSFilePosixPermissions".

此外, NSFilePosixPermissions参考表示关于NSFilePosixPermisions的信息:

Also, the NSFilePosixPermissions reference says about NSFilePosixPermisions:

对应的值是一个NSNumber对象.使用shortValue方法检索权限的整数值.

The corresponding value is an NSNumber object. Use the shortValue method to retrieve the integer value for the permissions.

设置POSIX权限的正确方法是:

The proper way to set POSIX permissions is:

// chmod permissions 777

// Swift
attributes[NSFilePosixPermissions] = 0o777

// Objective-C
[attributes setValue:[NSNumber numberWithShort:0777] 
             forKey:NSFilePosixPermissions];

这篇关于NSFileManager和NSFilePosixPermissions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆