NSSavePanel收到警告:“请使用NSSavePanel而不是NSApplication或NSWindow启动面板”

问题描述

运行在Xcode 11.1内置的macOS 10.15.6中,当我显示一个保存对话框时,我的应用突然(至少我刚刚注意到)在控制台上吐出这些警告:

WARNING: <NSSavePanel: 0x100e43250> found it necessary to prepare implicitly; please prepare panels using NSSavePanel rather than NSApplication or NSWindow.
WARNING: <NSSavePanel: 0x100e43250> found it necessary to start implicitly; please start panels using NSSavePanel rather than NSApplication or NSWindow.
WARNING: <NSSavePanel: 0x100e43250> running implicitly; please run panels using NSSavePanel rather than NSApplication.

问题是,我 am 使用NSSavePanel显示此对话框。这是整个方法。我看不出这是怎么回事。我没有使用任何不推荐使用的方法。 (在我遇到问题的情况下,windownil,因此会调用runModel。)

/**  Present a modal dialog (window==nil) or begin a sheet (window!=nil) that prompts the user to create a new archive.
 *
 * Attaches the rundancy picker view to the save panel so the user can choose the redundancy level.
 * Presents a warning if the user picks a volume that has size limitations.
 * If successful,invokes the handler with the URL of the new repository and its creation settings.
 * @param window window for sheet,or nil for modal dialog
 * @param title optional title
 * @param name optional default name
 * @param directory optional default directory
 * @param handler completion handler
 */
+ (void)newArchivePrompForWindow:(nullable NSWindow *)window
                       withTitle:(nullable Nsstring*)title
                            name:(nullable Nsstring*)name
                     inDirectory:(nullable NSURL*)directory
               completionHandler:(void (^)(NSURL* repositoryURL,NSDictionary* settings))handler
{
    NSSavePanel* savePanel = [NSSavePanel savePanel];
    
    savePanel.title = ( title ?: @"New Archive" );
    savePanel.allowedFileTypes = @[kRepositoryFileType];
//  savePanel.canSelectHiddenExtension = YES;
    savePanel.extensionHidden = YES;
    savePanel.prompt = @"Create";
    savePanel.nameFieldLabel = @"Archive:";
    if (directory!=nil)
        savePanel.directoryURL = directory;     // set optional default folder
    if (name!=nil)
        savePanel.nameFieldStringValue = name;  // set optional name
    
    // attach the redundancy picker controller
    RepositoryPickRedundancyViewController* redundancyAccessoryController;
    redundancyAccessoryController = [[RepositoryPickRedundancyViewController alloc] initWithNibName:@"RepositoryPickRedundancyView"
                                                                                             bundle:nil];
    [savePanel setAccessoryView:redundancyAccessoryController.view];
    redundancyAccessoryController.version = kDataRedundancyPickerSchemeDefault;
    redundancyAccessoryController.configurationIndex = kDataRedundancyPickerConfigIndexDefault;
    
    // Present the panel
    if (window!=nil)
        {
        // The dialog is attached to a window
        // Present a sheet attached to the window
        [savePanel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
            if (result==NSFileHandlingPanelOKButton)
                // User picked an archive: vet it and continue
                NewArchivePostProcessing(savePanel,window,redundancyAccessoryController,savePanel.URL,handler);
            else
                // canceled
                handler(nil,nil);
            }];
        }
    else
        {
        // No parent window: run a modal dialog
        if ([savePanel runModal]==NSModalResponSEOK)
            // User picked an archive: vet it and continue
            NewArchivePostProcessing(savePanel,nil,handler);
        else
            // canceled
            handler(nil,nil);
        }
}

有人知道这里发生了什么吗?

解决方法

如果您更换

if ([savePanel runModal]==NSModalResponseOK)
    ...

使用

[savePanel beginWithCompletionHandler:^(NSModalResponse result) {
    if (result==NSFileHandlingPanelOKButton)
        ...
    }];

消息消失。

我想这是试图鼓励使用更现代的API的一种微妙方式。

唯一的问题是,该消息是错误的,runModal没有被弃用,并且在任何地方都没有提及或记录该文档。这些天,这似乎是课程的标准。 :(