swift 3.1.1.-release on linux copyItem(atPath:toPath :)尚未实现

我正在使用swift ubuntu docker: https : //github.com/IBM-Swift/swift-ubuntu-docker

并尝试将一个文件从pathA复制到pathB。 在执行期间,我得到了致命的错误:

fatal error: copyItem(atPath:toPath:) is not yet implemented: file Foundation/NSFileManager.swift, line 376 Illegal instruction 

命令:

 # swift --version 

回复

 Swift version 3.1.1 (swift-3.1.1-RELEASE) Target: x86_64-unknown-linux-gnu 

在线我find了应该实施的信息:

https://bugs.swift.org/browse/SR-2639

有人可以帮忙吗? 谢谢!

对于Linux Foundation框架的Swift 3.1分支没有实现copyItem(atPath:toPath:)

 open func copyItem(atPath srcPath: String, toPath dstPath: String) throws { NSUnimplemented() } 

你可以例如做的是

 let fm = FileManager.default if let contents = fm.contents(atPath: srcPath) { if !fm.createFile(atPath: destPath, contents: contents, attributes: nil) { print("cannot write destination file") } } else { print("cannot read source file") } 

这是如何在master分支上实现copyItem(atPath:toPath:)的简化版本。

如果文件非常大,那么您可能需要以块的forms进行复制,而不是将整个文件读入内存,例如:

 guard let srcFile = FileHandle(forReadingAtPath: srcPath) else { fatalError("cannot open source file") } guard let destFile = FileHandle(forWritingAtPath: destPath) else { fatalError("cannot open destination file") } while case let data = srcFile.readData(ofLength: 1024 * 1024), data.count > 0 { destFile.write(data) } srcFile.closeFile() destFile.closeFile() 

谢谢,工作正常。 同时我发现另一种解决scheme也是可行的;-)

 let data = try Data.init(contentsOf: URL.init(fileURLWithPath: path)) guard FileManager.default.createFile(atPath: url.path, contents: data, attributes: nil) else { print("Can not read/create the file") return false }