NSDocument持有一个完整的文件夹?

har*_*n78 9 directory cocoa objective-c nsdocument

我很抱歉,如果这个论点已被涵盖,但经过一些研究后我发现没有什么精确的.

我需要创建一个基于文档的应用程序,其中文档实际上不是单个文件,而是目录中的文件的结构化集合.窗口将显示包含在文件夹中的pdf,具有特定的文件名,并使用文件夹中其他文件的信息进行丰富.我无法使用pdf注释来实现这一点,我真的需要将文件与pdf分开.

实现这一目标的最佳方法是什么?我找到的所有示例代码都使用单个文件..

小智 13

您可以NSFileWrapper在基于文档的应用程序中用作包目录.

在应用程序Info.plist文件中,声明您的文档类型是包或包(LSTypeIsPackage带值的键YES).

在您的NSDocument子类中,实现以下方法进行读写.在这个例子中,我假设相应的模型实例变量是pdfDatasignatureBitmapData,它们分别存储在MainDocument.pdf和SignatureBitmap.png下的包目录中.

- (BOOL)readFromFileWrapper:(NSFileWrapper *)dirWrapper
                     ofType:(NSString *)typeName
                      error:(NSError **)outError
{

    NSFileWrapper *wrapper;
    NSData *data;

    wrapper = [[dirWrapper fileWrappers] objectForKey:@"MainDocument.pdf"];
    data = [wrapper regularFileContents];
    self.pdfData = data;

    wrapper = [[dirWrapper fileWrappers] objectForKey:@"SignatureBitmap.png"];
    data = [wrapper regularFileContents];
    self.signatureBitmapData = data;

    …

    return YES;
}

- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
                               error:(NSError **)outError
{
    NSFileWrapper *dirWrapper = [[[NSFileWrapper alloc]
        initDirectoryWithFileWrappers:nil] autorelease];

    [dirWrapper addRegularFileWithContents:self.pdfData
        preferredFilename:@"MainDocument.pdf"];

    [dirWrapper addRegularFileWithContents:self.signatureBitmapData
        preferredFilename:@"SignatureBitmap.png"];

    …

    return dirWrapper;
}
Run Code Online (Sandbox Code Playgroud)

从用户的角度来看,包目录在Finder中显示为好像是单个文件,就像Xcode .xcodeproj目录或应用程序包一样.