个性化阅读
专注于IT技术分析

如何使用Swift 5在MacOS中实现文件和目录选择器

本文概述

作为你需要在每个平台的每个应用程序上实现的最常见和常规的内容之一, 你当然应该能够按照官方文档轻松地从系统中选择文件和目录。不幸的是, 没有太多的资料可以在MacOS中获得关于如何实现文件/目录选择器的指示或示例, 并且你发现的信息通常已经过时了, 因为它们没有提及它们是哪个版本的Swift。

在本文中, 我想与你分享一些示例, 说明如何使用我经常在应用程序上使用的Swift 5使用本机MacOS文件/目录选择器。

A.实现文件选择器

基本上所有的工作都由NSOpenPanel类处理。无需实现自己的文件浏览器, 而是使用”打开”面板类作为允许用户在系统中查找文件或目录的一种非常方便的方法。以下代码段介绍了显示此对话框和处理用户选择的最简单方法:

let dialog = NSOpenPanel();

dialog.title                   = "Choose a file| Our Code World";
dialog.showsResizeIndicator    = true;
dialog.showsHiddenFiles        = false;
dialog.allowsMultipleSelection = false;
dialog.canChooseDirectories = false;

if (dialog.runModal() ==  NSApplication.ModalResponse.OK) {
    let result = dialog.url // Pathname of the file

    if (result != nil) {
        let path: String = result!.path
        
        // path contains the file path e.g
        // /Users/ourcodeworld/Desktop/file.txt
    }
    
} else {
    // User clicked on "Cancel"
    return
}

你只需要创建NSOpenPanel的实例并调用runModal方法来显示它。以前, 你可能需要更改一些默认属性, 因为可以选择多个文件等。该方法将返回可以与NSApplication.ModalResponse结构(模式对话框的按钮返回值集)进行比较的代码。在某种条件下, 你应该检查用户是否选择了文件并单击”确定”, 最后你应该验证对话框实例的url属性是否为nil, 并且你将能够获取用户选择的文件的路径。

A.1。按文件扩展名过滤

在某些情况下, 用户不应选择系统中的任何文件, 而应选择特定格式的文件。例如, 常见的情况是图片处理软件的事实, 用户应该只能选择扩展名为png, jpg或jpeg的图片。以下代码段应显示一个文件选择器, 该文件选择器仅允许用户选择图像(如文章的图像):

let dialog = NSOpenPanel();

dialog.title                   = "Choose an image | Our Code World";
dialog.showsResizeIndicator    = true;
dialog.showsHiddenFiles        = false;
dialog.allowsMultipleSelection = false;
dialog.canChooseDirectories = false;
dialog.allowedFileTypes        = ["png", "jpg", "jpeg", "gif"];

if (dialog.runModal() ==  NSApplication.ModalResponse.OK) {
    let result = dialog.url // Pathname of the file

    if (result != nil) {
        let path: String = result!.path
        
        // path contains the file path e.g
        // /Users/ourcodeworld/Desktop/tiger.jpeg
    }
    
} else {
    // User clicked on "Cancel"
    return
}

A2。选择多个文件

如果要允许用户一次选择多个文件, 请确保将对话框的allowMultipleSelection选项设置为true。然后, 用户应该能够选择他想要的任何文件以及多少个文件。还要确保更改代码以操纵结果。不要使用dialog.url属性, 而是使用dialog.urls, 因为它包含具有选定文件的数组。你可以对结果执行任何操作, 使用path属性从数组的每个项目中提取路径, 如以下示例所示:

let dialog = NSOpenPanel();

dialog.title                   = "Choose multiple files | Our Code World";
dialog.showsResizeIndicator    = true;
dialog.showsHiddenFiles        = false;
dialog.canChooseDirectories    = false;
dialog.allowsMultipleSelection = true;

if (dialog.runModal() ==  NSApplication.ModalResponse.OK) {
    // Results contains an array with all the selected paths
    let results = dialog.urls
    
    // Do whatever you need with every selected file
    // in this case, print on the terminal every path
    for result in results {
        // /Users/ourcodeworld/Desktop/fileA.txt
        print(result.path)
    }
} else {
    // User clicked on "Cancel"
    return
}

B.实现目录选择器

为了实现目录选择器, 我们将使用相同的NSOpenPanel类, 但是在初始化对话框时, 你需要将以下两个属性更改为对话框:

dialog.canChooseFiles = false;
dialog.canChooseDirectories = true;

这将允许用户仅选择系统中的目录。其余逻辑与文​​件选择器基本相同。例如:

B.1。选择一个目录

以下代码段显示了选择器的实现, 该选择器仅允许用户选择一个目录:

let dialog = NSOpenPanel();

dialog.title                   = "Choose single directory | Our Code World";
dialog.showsResizeIndicator    = true;
dialog.showsHiddenFiles        = false;
dialog.canChooseFiles = false;
dialog.canChooseDirectories = true;

if (dialog.runModal() ==  NSApplication.ModalResponse.OK) {
    let result = dialog.url

    if (result != nil) {
        let path: String = result!.path
        
        // path contains the directory path e.g
        // /Users/ourcodeworld/Desktop/folder
    }
} else {
    // User clicked on "Cancel"
    return
}

B.2。选择多个目录

以下代码段显示了选择器的实现, 该选择器允许用户同时选择多个目录:

let dialog = NSOpenPanel();

dialog.title                   = "Choose multiple directories | Our Code World";
dialog.showsResizeIndicator    = true;
dialog.showsHiddenFiles        = false;
dialog.allowsMultipleSelection = true;
dialog.canChooseFiles = false;
dialog.canChooseDirectories = true;

if (dialog.runModal() ==  NSApplication.ModalResponse.OK) {
    
    // Results contains an array with all the selected paths
    let results = dialog.urls
    
    // Do whatever you need with every selected file
    // in this case, print on the terminal every path
    for result in results {
        // /Users/ourcodeworld/Desktop/folderA
        print(result.path)
    }
} else {
    // User clicked on "Cancel"
    return
}

编码愉快!

赞(2)
未经允许不得转载:srcmini » 如何使用Swift 5在MacOS中实现文件和目录选择器

评论 抢沙发

评论前必须登录!