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

标签 swift linux docker containers

我正在使用 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/

相关文章:

ios - 如何在表格 View 底部添加新行 - 聊天消息

ios - 将视频分享到 Facebook

linux - OpenMutex 和 pthread

hadoop - 从另一个运行 Spark 的 Docker 容器写入在 Docker 中运行的 HDFS

mongodb - Studio 3T如何连接MongoDB docker?

ios - UIView 动画在 iOS 8 中无法正常工作?

swift - 如何解决 Swift 编译器错误 "error: Bus error: 10"?

linux - 如何在编写空白 bash 脚本后提取所有内容

Linux 删除至少 2 周前的文件 AND (day of month) % 14 != 0

php - Wordpress 容器内的 SSH 失败