Macos 使用NSOpenPanel获取文件夹位置并删除file://localhost

Macos 使用NSOpenPanel获取文件夹位置并删除file://localhost,macos,cocoa,nsopenpanel,Macos,Cocoa,Nsopenpanel,我正在使用NSOpenPanel在我的应用程序上设置一个文件夹,但我将其作为文件夹的路径“file://localhost/Users/juan/" 这是我的代码: NSOpenPanel * setDirectory = [NSOpenPanel openPanel]; setDirectory.canChooseFiles=NO; setDirectory.canCreateDirectories=YES; setDirectory.canChooseDire

我正在使用NSOpenPanel在我的应用程序上设置一个文件夹,但我将其作为文件夹的路径“file://localhost/Users/juan/"

这是我的代码:

    NSOpenPanel * setDirectory = [NSOpenPanel openPanel];
    setDirectory.canChooseFiles=NO;
    setDirectory.canCreateDirectories=YES;
    setDirectory.canChooseDirectories=YES;
    setDirectory.directoryURL=_path;
    [setDirectory beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
        NSLog(@" results %ld", result);
        if (NSFileHandlingPanelCancelButton){
            NSLog(@"cancel");

        }
        if (NSFileHandlingPanelOKButton) {

            self.path = [[setDirectory URLs] objectAtIndex:0];

        }

    }];
}
我的问题是如何或需要做什么来获得绝对路径,例如“/Users/juan/Desktop/”而不需要file://localhost.


我真的很感激任何指点

要执行此操作,请调用
NSURL
对象的
path
方法。

我发现了使用
[bitmapData writeToFile:name和path原子性:YES]时遇到的问题它给了我一个错误。

我切换到
[bitmapData writeToURL:nameandPath原子性:是]一切都很好。

尽管你现在真的应该只使用URL。您仍然必须使用路径的项目数量很小并且在不断减少。如果用户选择多个项目是有意义的,为什么您只处理用户选择的随机项目?如果没有意义,为什么不关闭
allowsMultipleSelection
?区别在于
writeToFile::
将路径作为NSString对象,而
writeToURL::
将URL作为NSURL对象。URL现在是引用文件的首选方式。顺便说一下,这两种方法都不推荐使用;您应该改为使用
writeToURL:options:error:
,并在错误失败时处理该错误。