【问题标题】:NSDocument and NSFileWrapping saving bad namesNSDocument 和 NSFileWrapping 保存坏名
【发布时间】:2013-08-07 14:53:26
【问题描述】:

我的 NSDocument 子类使用使用 fileWrapperOfType:error: 将数据写入磁盘的捆绑文档类型。这非常有效,但最终它会开始保存带有乱码名称的文件,例如“2_#$!@%!#_info.plist”,而不是覆盖正确的文件。

我不知道导致问题的更改是什么,但我认为它与权限相关,因为即使“stat”返回所有者可写的“drwxr-xr-x”,Finder 也不允许我在包中写入。

是 NSFileWrapper 或 NSDocument 搞砸了权限还是什么?我是这个 API 的新手,但这是我在下面使用的代码(在 Objective Pascal 中),希望所有人都能阅读。

function TScriptDocument.fileWrapperOfType_error (typeName: NSString; outError: NSErrorPtr): NSFileWrapper;
var
    fileWrappers: NSDictionary;
    propertiesFileWrapper: NSFileWrapper;
    data: NSData;
    propertiesName: NSString;
begin
    if documentFileWrapper = nil then
        documentFileWrapper := NSFileWrapper.alloc.initDirectoryWithFileWrappers(nil);      

    fileWrappers := documentFileWrapper.fileWrappers;
    propertiesName := NSSTR('info.plist');
    propertiesFileWrapper := fileWrappers.objectForKey(propertiesName);
    if propertiesFileWrapper <> nil then
        documentFileWrapper.removeFileWrapper(propertiesFileWrapper);
    data := NSPropertyListSerialization.dataWithPropertyList_format_options_error(script.GetProperties, NSPropertyListXMLFormat_v1_0, 0, nil);
    if data <> nil then
        documentFileWrapper.addRegularFileWithContents_preferredFileName(data, propertiesName);

    result := documentFileWrapper;
end;

【问题讨论】:

    标签: objective-c cocoa nsdocument nsfilewrapper


    【解决方案1】:

    我发现在对 fileWrapperOfType_error 的调用中保留来自 initDirectoryWithFileWrappers 的文件包装器会导致问题。现在,每次使用 initWithURL_options_error 或 initDirectoryWithFileWrappers 保存文档时,我都会初始化一个新的 NSFileWrapper 实例。

    但是,即使我对目录 0777 进行了 chmod,我也无法修改 Finder 中的文档包内容,这仍然是一个问题。也许它对这些文件施加了一些特定于 Finder 的限制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多