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

Swift 在HTTP请求中写入静态字典时的未捕获豁免,swift,dictionary,Swift,Dictionary,我在http闭包中得到了一个未捕获的豁免,该闭包与一个声明存在未捕获豁免的字典相关。当我设置断点时,它指向一个字典。所讨论的字典在结构中声明为静态变量,并且已经有多个值,那么这怎么可能发生呢?这是http请求 session.dataTask(with: request){ (data, response, error) in if let data = data, let tile = UIImage(data: data),

我在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()
会话中的
session.dataTask(带:请求){(数据、响应、错误)
如果let data=data,
让平铺=UIImage(数据:数据),
让documentsURL=FileManager.default.url(对于:.documentDirectory,在:.userDomainMask中)。首先{
让fileName=Date().timeIntervalSince1970
让filePath=documentsURL.appendingPathComponent(字符串(描述:文件名))
Maps.tileCachePath[url]=fileName/这是一个打字错误

替换

Maps.tileCachePath[url] = fileName

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

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

date()
是双精度的,您可能需要一个字符串值。

尝试两种解决方案后(将双精度转换为字符串并用实际文件名替换双精度),我仍然会遇到崩溃。如果我为未捕获的异常禁用断点,有时是EXC\u BAD\u访问,有时是异常。不确定是否有帮助,但异常为“NSInvalidArgumentException”,原因:'-[\u NSCFNumber count]:未识别的选择器发送到实例
Maps.tileCachePath[url] = filePath