我正在使用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
在线我发现了应该实施的信息:
https://bugs.swift.org/browse/SR-2639
有人可以帮忙吗?谢谢!
答案 0 :(得分:1)
copyItem(atPath:toPath:)
未在Swift 3.1 branch上实施
Linux的Foundation框架:
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")
}
这是copyItem(atPath:toPath:)
的简化版本
已在master branch上实施。
如果文件非常大,那么您可能希望以块的形式进行复制 而不是将整个文件读入内存,例如:
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()
答案 1 :(得分:0)
谢谢你的工作正常。我发现同时另一个也有效的解决方案; - )
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
}