URL 包含正斜杠不适用于 NSWorkSpace

URL Containing forward slash doesn't work with NSWorkSpace

系统中存在带正斜杠(/) 的文件名。

For example: URL -> ~/Documents/FolderName/TestFilename/myFile.dmg.

Last Path Component is -> "TestFilename/myFile.dmg"

File Name is -> "TestFilename/myFile.dmg"

现在,当我在我的应用程序中使用下面的代码来在 finder 中显示以下代码时。它无法在 finder 中显示。

NSURL *fileURL = [NSURL URLWithString:@"/Documents/FolderName/TestFilename/myFile.dmg"];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[fileURL]];

现在如何解决这种情况并使其在此类文件的查找器中显示。我确实尝试过 "CFURLCreateStringByAddingPercentEscapes",它似乎不起作用。 .

如果您可以在代码中分离文件名和文件位置,就可以实现这一点。

示例代码如下:

NSString *location = @"Users/Desktop";
NSString *fileName = @"TestFilename/myFile.dmg";
if ([fileName rangeOfString:@"/"].location != NSNotFound)
{
       fileName = [fileName stringByReplacingOccurrencesOfString:@"/" withString:@":"];
}

[[NSWorkspace sharedWorkspace] selectFile:[location stringByAppendingPathComponent:fileName] inFileViewerRootedAtPath:location];