2015-04-06 72 views
2

我有一个使用NSFileManager创建的文件,但找不到使其只读的方法。我在互联网和苹果的NSFileManager Class Reference搜索了所有,但找不到任何东西。以下是创建文件的代码。NSFileManager - 创建只读文件

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

回答

4

使用setAttributes:[NSFileManager defaultManager]删除文件读取位。 This帖子解释它。

将以下权限设置为-rwxrwxrwx(0777)。用你想要的权限替换0777。

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

太谢谢你了。我对此很陌生,所以你可以提供一个使用setAttributes的例子 – Maanit

+0

是的!我已经更新了我的答案。 –

+0

谢谢,这工作完美。 – Maanit

1

用途:

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

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