如何使用swift filemanager访问外部硬盘驱动器
我正在尝试使用Swift 5.0 FileManager访问连接到Mac的外部硬盘上的文件。应用程序提示用户输入文件夹,读取所有图像文件,并根据exif数据进行分类 我有一个NSOpenPanel(),它允许用户选择目录并获取该目录中的文件如何使用swift filemanager访问外部硬盘驱动器,swift,macos,Swift,Macos,我正在尝试使用Swift 5.0 FileManager访问连接到Mac的外部硬盘上的文件。应用程序提示用户输入文件夹,读取所有图像文件,并根据exif数据进行分类 我有一个NSOpenPanel(),它允许用户选择目录并获取该目录中的文件 @IBAction func browseFile(sender: AnyObject) { let dialog = NSOpenPanel(); dialog.title = "Ch
@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
}
}
该程序适用于计算机上的文件,但每当我试图选择连接的外部硬盘时,它就会崩溃。它在哪里崩溃?代码看起来非常客观。至少删除后面的分号。您可以将
[URLResourceKey.creationDateKey.self]
替换为[.creationDateKey]
。您是说,如果选择内部驱动器上的文件夹,代码可以正常工作吗?@JoakimDanielson是的,如果选择内部驱动器上的文件夹,代码可以正常工作,但选择外部usb硬盘上的文件夹时,代码会崩溃。