gpt4 book ai didi

swift - 升级到 Xcode 11 后,Bundle 资源的 URL 不再起作用

转载 作者:行者123 更新时间:2023-11-30 10:35:43 27 4
gpt4 key购买 nike

我将 Xcode 升级到版本 11.0 (11A420a),部分代码无法像以前一样工作。我想将文件夹从 Bundle 复制到 TemporaryDirectory。

    func makeHtmlFile (type: InvoiceType?) -> URL {
let identifier = Bundle.main.bundleIdentifier!
let cacheDirectory = NSTemporaryDirectory() + "\(identifier)/Website/"
let cacheURL = URL(fileURLWithPath: cacheDirectory )
let websiteUrl = URL(fileReferenceLiteralResourceName: "Website")
do {
try FileManager().copyItem(at: websiteUrl, to: cacheURL)
} catch let error {
print ("Copy \(error)\n\tWebsite: \(websiteUrl)\n\tCache:\(cacheURL)")
}
...
}

在 Xcode 10 中,这部分代码可以在包内找到文件夹 /Website ,在 temporary directory 中找到文件夹 /var/folders/.... ,但升级后会打印错误:

Copy Error Domain=NSCocoaErrorDomain Code=4 "The file “Website” doesn’t exist."

错误消息中显示的路径存在,我可以通过从错误消息中复制它们并粘贴到 Finder -> Go -> Go to Folder 来到达那里。有人知道升级后发生了什么变化吗?

最佳答案

好吧,我至少为自己解决了问题。我没有使用 NSTemporaryDirectory(),而是使用 FileManager().temporaryDirectory 作为临时文件 URL 和

let tempDirectory = FileManager().temporaryDirectory
if let websiteURL = Bundle.main.url(forResource: "Website", withExtension: nil) {
let cacheURL = tempDirectory.appendingPathComponent(websiteURL.lastPathComponent, isDirectory: true)
...
}

似乎可以像升级之前一样工作。

关于swift - 升级到 Xcode 11 后,Bundle 资源的 URL 不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58080243/

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