问题描述
我想为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
:
设置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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!