FileManager.default.copyItem 抛出错误“文件不存在”

问题描述 投票:0回答:1

我是 SwiftUI 的初学者,我的练习代码 repo 在这里。我正在尝试使用 LLMFarm Core 创建本地法学硕士。在尝试运行此代码时

try FileManager.default.copyItem(atPath: temporaryURL.path, toPath: fileURL.path)
我收到错误,我猜它来自 temporURL 变量:
Error: The file “CFNetworkDownload_rr8uC6.tmp” doesn’t exist.

这就是我的代码函数的样子

status = "downloading"
print("Downloading model \(modelName) from \(modelUrl)")
guard let url = URL(string: modelUrl) else { return }
let fileURL = getFileURLFormPathStr(dir:"models",filename: filename)
downloadTask = URLSession.shared.downloadTask(with: url) { temporaryURL, response, error in
    if let error = error {
        return
    }
    guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else {
        print("Server error!")
        return
    }
    do {
        if let temporaryURL = temporaryURL {
            try FileManager.default.copyItem(atPath: temporaryURL.path, toPath: fileURL.path)

我尝试使用

try FileManager.default.copyItem(at: temporaryURL, to: fileURL)
代替,但这也不起作用。 我还确保已使用 LLM farm 示例添加了似乎需要的所有权利和信息 plist 属性。任何帮助将不胜感激。该存储库应该易于克隆,并且您可以在本地计算机上对其进行测试。我在本地个人设备和M1电脑上尝试过,但得到了同样的错误

编辑:我还尝试在

if !FileManager.default.fileExists(atPath: fileURL.path)
上方添加
if let temporaryURL = temporaryURL
,因此该文件存在,但我不知道为什么我的代码没有反映这一点。

Edit2:这是临时URL的值

file:///Users/salman/Library/Containers/com.guinmoon.LLMFarm23/Data/tmp/CFNetworkDownload_QfLexg.tmp
这是 fileURL 的值
file:///Users/salman/Library/Containers/com.guinmoon.LLMFarm23/Data/Documents/models/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf

ios swift large-language-model
1个回答
0
投票

看起来

FileManager.copyItem(atPath:toPath:)
抛出了一个误导性错误。

临时文件确实存在于

temporaryURL
。但是,目标目录
file:///Users/salman/Library/Containers/com.guinmoon.LLMFarm23/Data/Documents/models
不存在。由于
copyItem(atPath:toPath:)
不会自动创建新文件所需的目录,因此尝试将文件复制到
fileURL
失败。

开始下载之前,通过调用

models
创建
try FileManager.default.createDirectory(at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true)
目录,复制操作应该会成功。

© www.soinside.com 2019 - 2024. All rights reserved.