文档类型图标未显示在自定义文件 MacOS 上

2024-04-16

我的应用程序需要有自定义文档支持,但我并没有从 xcode 中的文档模板开始。我确实通过遵循本教程的其他内容成功地正确加载和保存数据:https://www.brandpending.com/2016/02/21/opening-and- saving-custom-document-types-from-a-swift-cocoa-application/ https://www.brandpending.com/2016/02/21/opening-and-saving-custom-document-types-from-a-swift-cocoa-application/。但有一个问题是我的自定义图标没有显示在我的文件上!它仍然是一个空白页面图标:(。

我究竟做错了什么?

这是我的设置:

我的 HSDocument 代码:

-(BOOL)readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError
{
    NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver   unarchiveObjectWithFile:[url path]];
    self.archive = myDictionary;

    return YES;
}

-(BOOL)writeToURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError
{
    [NSKeyedArchiver archiveRootObject:self.archive toFile:[url path]];

  return YES;
}

这是我如何将数据加载/保存到文件中:(我不希望用普通菜单项触发它)

- (void)onLoadFile
{
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setCanChooseFiles:YES];
    [panel setCanChooseDirectories:YES];
    [panel makeKeyAndOrderFront:nil];
    [panel setLevel:NSStatusWindowLevel];
    [panel setCanCreateDirectories:NO];


    [[self.preferencesWindow window] setLevel:kCGNormalWindowLevel];

    [panel setFrameOrigin:CGPointZero];

    [panel beginWithCompletionHandler:^(NSInteger result)
    {
        if (result == NSFileHandlingPanelOKButton)
        {
            for (NSURL *url in [panel URLs])
            {
                NSError *error;
                HSDocument *document = [HSDocument new];

                [document readFromURL:url ofType:@"my-identifier" error:&error];
                [self setPresetsFromCollection:document.archive onStartup:NO];
            }
        }

        [[self.preferencesWindow window] setLevel:NSStatusWindowLevel];
    }];
}

- (void)onSaveFile
{
    NSSavePanel *panel = [NSSavePanel savePanel];
    [panel setMessage:@"Please select a location to save the file."];
    [panel setAllowsOtherFileTypes:YES];
    [panel setExtensionHidden:YES];
    [panel setTitle:@"Saving file..."]; // Window title
    [panel setCanCreateDirectories:YES];

    [[self.preferencesWindow window] setLevel:kCGNormalWindowLevel];

    [panel beginWithCompletionHandler:^(NSInteger result)
    {
        if (result == NSFileHandlingPanelOKButton)
        {
            NSURL* theUrl = [panel URL];
            theUrl = [theUrl URLByAppendingPathExtension:@"hej"];

            HSDocument *document = [HSDocument new];
            document.archive = [self collectPresetsToSave];
            NSError *error;
            [document writeToURL:theUrl ofType:@"my-identifier" error:&error];
        }

        [[self.preferencesWindow window] setLevel:NSStatusWindowLevel];
    }];
}

Thnx!


在 Mojave、Xcode 10.1 中也有同样的问题。

最终解决问题的是使用 .ICNS 文件作为图标文件类型。只要我尝试使用 PNG 文件,这些文件就不会显示在 Finder 中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

文档类型图标未显示在自定义文件 MacOS 上 的相关文章

随机推荐