gpt4 book ai didi

swift 3.1.1.-在 linux copyItem 上发布(atPath :toPath:) is not yet implemented

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:56 25 4
gpt4 key购买 nike

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

然后尝试将文件从路径 A 复制到路径 B。在执行过程中我得到了 fatal error :

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

有人可以帮忙吗?谢谢!

最佳答案

copyItem(atPath:toPath:) 未在 Swift 3.1 branch 上实现Linux 的基础框架:

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()

关于swift 3.1.1.-在 linux copyItem 上发布(atPath :toPath:) is not yet implemented,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44741037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com