使用 FileManager 复制文件时出错(CFURLCopyResourcePropertyForKey 失败,因为它传递了一个没有方案的 URL)
Error copying files with FileManager (CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme)
我正在尝试使用 FileManager
的 copyItem(at:path:)
将一些(媒体)文件从一个文件夹复制到另一个文件夹,但出现错误:
CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
Error Domain=NSCocoaErrorDomain Code=262 "The file couldn’t be opened because the specified URL type isn’t supported."
我正在使用 Xcode 9 beta 和 Swift 4.
let fileManager = FileManager.default
let allowedMediaFiles = ["mp4", "avi"]
func isMediaFile(_ file: URL) -> Bool {
return allowedMediaFiles.contains(file.pathExtension)
}
func getMediaFiles(from folder: URL) -> [URL] {
guard let enumerator = fileManager.enumerator(at: folder, includingPropertiesForKeys: []) else { return [] }
return enumerator.allObjects
.flatMap {[=11=] as? URL}
.filter { [=11=].lastPathComponent.first != "." && isMediaFile([=11=])
}
}
func move(files: [URL], to location: URL) {
do {
for fileURL in files {
try fileManager.copyItem(at: fileURL, to: location)
}
} catch (let error) {
print(error)
}
}
let mediaFilesURL = URL(string: "/Users/xxx/Desktop/Media/")!
let moveToFolder = URL(string: "/Users/xxx/Desktop/NewFolder/")!
let mediaFiles = getMediaFiles(from: mediaFilesURL)
move(files: mediaFiles, to: moveToFolder)
错误发生是因为
URL(string: "/Users/xxx/Desktop/Media/")!
创建一个没有方案的 URL。您可以使用
URL(string: "file:///Users/xxx/Desktop/Media/")!
或者,更简单地说,
URL(fileURLWithPath: "/Users/xxx/Desktop/Media/")
另请注意,在 fileManager.copyItem()
中,目的地必须
包括文件名,而不仅仅是目的地
目录:
try fileManager.copyItem(at: fileURL,
to: location.appendingPathComponent(fileURL.lastPathComponent))
我正在尝试使用 FileManager
的 copyItem(at:path:)
将一些(媒体)文件从一个文件夹复制到另一个文件夹,但出现错误:
CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme Error Domain=NSCocoaErrorDomain Code=262 "The file couldn’t be opened because the specified URL type isn’t supported."
我正在使用 Xcode 9 beta 和 Swift 4.
let fileManager = FileManager.default
let allowedMediaFiles = ["mp4", "avi"]
func isMediaFile(_ file: URL) -> Bool {
return allowedMediaFiles.contains(file.pathExtension)
}
func getMediaFiles(from folder: URL) -> [URL] {
guard let enumerator = fileManager.enumerator(at: folder, includingPropertiesForKeys: []) else { return [] }
return enumerator.allObjects
.flatMap {[=11=] as? URL}
.filter { [=11=].lastPathComponent.first != "." && isMediaFile([=11=])
}
}
func move(files: [URL], to location: URL) {
do {
for fileURL in files {
try fileManager.copyItem(at: fileURL, to: location)
}
} catch (let error) {
print(error)
}
}
let mediaFilesURL = URL(string: "/Users/xxx/Desktop/Media/")!
let moveToFolder = URL(string: "/Users/xxx/Desktop/NewFolder/")!
let mediaFiles = getMediaFiles(from: mediaFilesURL)
move(files: mediaFiles, to: moveToFolder)
错误发生是因为
URL(string: "/Users/xxx/Desktop/Media/")!
创建一个没有方案的 URL。您可以使用
URL(string: "file:///Users/xxx/Desktop/Media/")!
或者,更简单地说,
URL(fileURLWithPath: "/Users/xxx/Desktop/Media/")
另请注意,在 fileManager.copyItem()
中,目的地必须
包括文件名,而不仅仅是目的地
目录:
try fileManager.copyItem(at: fileURL,
to: location.appendingPathComponent(fileURL.lastPathComponent))