objective-c - NSFileManager - 创建一个只读文件

标签 objective-c readonly nsfilemanager

我有一个使用 NSFileManager 创建的文件,但找不到将其设为只读的方法。我在整个互联网和 Apple 的 NSFileManager Class Reference 中进行了搜索但找不到任何东西。这是文件创建位置的代码。

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

最佳答案

使用 [NSFileManager defaultManager]setAttributes: 删除文件读取位。 This帖子对此进行了解释。

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

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

关于objective-c - NSFileManager - 创建一个只读文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29476096/

相关文章:

ios - NSURLProtocol - canInitWithRequest 为页面资源返回 YES 后未调用 startLoading

swift - 如何在 Swift 3 中使用 URL resourceValues 方法获取文件创建日期?

objective-c - 我将如何解析 Objective-C 中的相对路径?

ios - 在 iOS 中使用 NSFileManager 写入文件

objective-c - 在 Objective C 中格式化字符串

objective-c - 在@implementation 而不是@interface 定义的类变量?

objective-c - 如何处理 NSTextField 子类的第一响应者状态?

jquery - 将网页设置为只读?

C# RichTextBox ReadOnly 事件

C# 错误消息 "Property can not be assigned to -- it is read only."