gpt4 book ai didi

swift - 在 HTTP 请求中写入静态字典时未捕获的豁免

转载 作者:行者123 更新时间:2023-11-28 13:45:46 25 4
gpt4 key购买 nike

我在与字典相关的 http 闭包中获得未捕获豁免,说明存在未捕获豁免。当我设置一个断点豁免时,它指向一个字典。有问题的字典在结构中声明为静态变量,并且其中已经有多个值,所以这怎么会发生呢?这是 http 请求。

 session.dataTask(with: request){ (data, response, error) in
if let data = data,
let tile = UIImage(data: data),
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first{
let fileName = Date().timeIntervalSince1970
let filePath = documentsURL.appendingPathComponent(String(describing: fileName))


Maps.tileCachePath[url] = fileName //<- this is where the exception happens


//make sure there is no old file and if so delete it
if FileManager.default.fileExists(atPath: filePath.path){
do {
try FileManager.default.removeItem(at: filePath)

} catch{
print("error deleting old tile")
}

}

//now write the new file
FileManager.default.createFile(atPath: filePath.path, contents: data, attributes: nil)
print(filePath.path)
//return
result(tile, error)

} else {
result(nil, error)
}


}.resume()

最佳答案

这是一个错字

替换

Maps.tileCachePath[url] = fileName

Maps.tileCachePath[url] = filePath

基本上 Date().timeIntervalSince1970 作为文件名是一个非常糟糕的主意。该数字包含被视为文件扩展名的小数秒。

使用更可靠的文件名,如格式化日期或至少删除小数秒并添加真实文件扩展名。

关于swift - 在 HTTP 请求中写入静态字典时未捕获的豁免,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55403146/

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