简体   繁体   English

NSFileManager-创建一个只读文件

[英]NSFileManager - Create a readonly file

I have a file which I created using NSFileManager but could not find a way to make it readonly. 我有一个使用NSFileManager创建的文件,但找不到将其设置为只读的方法。 I have searched all over the internet and in Apple's NSFileManager Class Reference but could not find anything. 我已经在整个互联网和Apple的NSFileManager类参考中进行了搜索,但找不到任何内容。 Here is the code of where the file was created. 这是文件创建位置的代码。

if ([fm createFileAtPath:fileName contents:inputData attributes:nil] == NO) {
     NSLog(@"Error!");
     return 1;
}

Use setAttributes: of [NSFileManager defaultManager] to remove the files read bit. 使用[NSFileManager defaultManager] setAttributes:删除文件读取位。 This posts explains it. 这篇文章对此进行了解释。

Following sets permissions to -rwxrwxrwx (0777). 以下将权限设置为-rwxrwxrwx(0777)。 Replace 0777 with the permissions you desire. 将0777替换为所需的权限。

NSDictionary *attributes;
[attributes setValue:[NSNumber numberWithShort:0777] forKey:NSFilePosixPermissions];
[[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:@"/path/to/file"]

Use : 采用 :

[attributes setValue:[NSNumber numberWithShort:0777] forKey:NSFilePosixPermissions];

[filemanager setAttributes:atributes ofItemAtPath:yourPath error:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM