【问题标题】:NSFileManager - Create a readonly fileNSFileManager - 创建一个只读文件
【发布时间】:2015-04-06 17:20:25
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c readonly nsfilemanager


    【解决方案1】:

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

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

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

    【讨论】:

    • 非常感谢。我是新手,所以您能否提供一个使用setAttributes 的示例,最好在此示例中使用
    【解决方案2】:

    使用:

    [attributes setValue:[NSNumber numberWithShort:0777] forKey:NSFilePosixPermissions];
    
    [filemanager setAttributes:atributes ofItemAtPath:yourPath error:nil];
    

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      相关资源
      最近更新 更多