如何使用swift文件管理器访问外部硬盘驱动器

时间:2019-09-23 12:10:00

标签: swift macos

我正在尝试使用Swift 5.0 FileManager访问连接到Mac的外部硬盘驱动器上的文件。该应用程序提示用户输入文件夹,读取所有图像文件,并根据exif数据进行分类。

我有一个NSOpenPanel(),它允许用户选择目录并获取该目录中的文件

@IBAction func browseFile(sender: AnyObject) {

        let dialog = NSOpenPanel();

        dialog.title                   = "Choose a .txt file";
        dialog.showsResizeIndicator    = true;
        dialog.showsHiddenFiles        = false;
        dialog.canChooseDirectories    = true;
        dialog.canCreateDirectories    = true;
        dialog.allowsMultipleSelection = true;
        dialog.canChooseFiles = false;

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

            if (result != nil) {
                originalPhotoDirectory = result!
                originalDirectoryLabel.stringValue = result!.path
                //basic gist of the app
                let directoryContents = try FileManager.default.contentsOfDirectory(at: result,includingPropertiesForKeys: [URLResourceKey.creationDateKey.self]);
                //then loops through files in directory and gets exif

            }
        } else {
            // User clicked on "Cancel"
            return
        }

    }

该程序可以很好地处理计算机上的文件,但是每当我尝试选择已连接的外部硬盘驱动器时,它就会崩溃。

0 个答案:

没有答案
相关问题