C语言中的等价代码是什么:
chmod(MY_FILE, 0777);
在Objective C中如何写入已锁定的文件而不执行
chmod +x MY_FILE
在终端上。
C语言中的等价代码是什么:
chmod(MY_FILE, 0777);
在Objective C中如何写入已锁定的文件而不执行
chmod +x MY_FILE
您可以使用Cocoa的-setAttributes:ofItemAtPath:error:
方法来完成这个任务。
[[NSFileManager defaultManager] setAttributes:@{ NSFilePosixPermissions : @0666 }
ofItemAtPath:…
error:&error];
您可以使用C语言的chmod()
函数。
在终端中输入man 2 chmod
查看相关文档和函数。
你可以使用Cocoa来实现这个。
NSTask *changePerms = [[NSTask alloc] init];
[changePerms setLaunchPath:@"/bin/chmod"];
NSArray *chmodArgs = [NSArray arrayWithObjects:@"666", @"/Users/abc/hello.txt", nil];
[changePerms setArguments:chmodArgs];
[changePerms launch];
这是使用NSFileManager类的另一种方式
NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithShort:0766], NSFilePosixPermissions, NULL];
NSError *error = nil;
[[NSFileManager defaultManager] setAttributes:attr ofItemAtPath:@"/Users/abc/Desktop/test.txt" error:&error];
使用system()调用
system("chmod 777 /Users/abc/Desktop/test.txt");
chmod()
函数随手可得时,调用外部命令没有任何意义。 - nneonneo[NSNumber numberWithUnsignedLong:___]
,因为chmod可以处理更大的值。 为了证明,请在文件上执行chmod u+s
,然后执行stat -r
并查看第三个参数。(至少在我的Mac上是第三个。)您会发现它是一个比有符号短整型可以容纳的数字大的数字,即32768,以及无符号短整型可以容纳的数字,即65535。将相同的数字与chmod一起传递到另一个测试文件中,您将看到chmod接受此无符号长整型值。 - Volomike
int NSArray;
也许不行 :-) - paxdiablo