xcode 展开可选值时意外发现nil,即使该值已分配值

sulc1iza  于 2023-02-09  发布在  其他
关注(0)|答案(3)|浏览(126)

我正在使用Swift从冠状病毒API获取JSON。但是当我尝试运行代码时,我得到了这个错误。
致命错误:展开可选值时意外发现nil:第22行
我的代码是

override func viewDidLoad() {
        super.viewDidLoad()
        
        let url = "https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=nation;areaName=england&structure={%22date%22:%22date%22,%22areaName%22:%22areaName%22,%22areaCode%22:%22areaCode%22,%22newCasesByPublishDate%22:%22newCasesByPublishDate%22,%22cumCasesByPublishDate%22:%22cumCasesByPublishDate%22,%22newDeathsByDeathDate%22:%22newDeathsByDeathDate%22,%22cumDeathsByDeathDate%22:%22cumDeathsByDeathDate%22}"
        getData(from: url)
        // Do any additional setup after loading the view.
    }
    
    private func getData(from url: String) {
        
        let getfromurl = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: {data, response, error in
            guard let data = data, error == nil else{
                print("Something Went Wrong")
                return
            }
            
            //Have data
            var result: Response?
            do {
                result = try JSONDecoder().decode(Response.self, from: data)
            }
            catch{
                print("failed to convert \(error.localizedDescription)")
            }
            
            guard let json = result else {
                return
            }
            
            print(json.data.date)
        })
        getfromurl.resume()
    
    }

第22行为:

let getfromurl = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: {data, response, error in

我很困惑,因为我认为这意味着url没有分配任何内容,但即使调试器也认为它有。

    • 更新日期:**

我可以获取数据,但获取后出现错误。错误为:
无法转换值NotFound(Swift.Int、Swift.DecodingError.上下文(编码路径:[编码键(字符串值:"数据",整数值:nil)、_JSONKey(字符串值:"索引0",整数值:0)、编码键(字符串值:"按死亡日期的新死亡",整数值:nil)],调试说明:"应为Int值,但找到null。",underlyingError:无))
转换失败表明在解码JSON和值时出错。

mw3dktmi

mw3dktmi1#

该异常并不意味着url就是nil,但是URL(string:url)就是nil
您需要检查url字符串是否为有效的url:

private func getData(from url: String) {
    guard let theURL = URL(string: url) else { print ("oops"); return }
    let getfromurl = URLSession.shared.dataTask(with: theURL, completionHandler: {data, response, error in
       /* ... */
    }
}

更新

由于现在给出了url字符串:问题出在花括号上它们在RFC1738中被标记为 * unsafe *,并且应该被%7b(打开)和%7d(关闭)替换,因此:

let url = "https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=nation;areaName=england&structure=%7b%22date%22:%22date%22,%22areaName%22:%22areaName%22,%22areaCode%22:%22areaCode%22,%22newCasesByPublishDate%22:%22newCasesByPublishDate%22,%22cumCasesByPublishDate%22:%22cumCasesByPublishDate%22,%22newDeathsByDeathDate%22:%22newDeathsByDeathDate%22,%22cumDeathsByDeathDate%22:%22cumDeathsByDeathDate%22%7d"

let theUrl = URL(string: url)
print (theUrl)

这也是official API documentation中的一个示例:

curl -si 'https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=nation;areaName=england&structure=%7B%22name%22:%22areaName%22%7D'
bn31dyow

bn31dyow2#

URL编码不正确。浏览器有自己的方式来编码URL,但URL(string:根本不进行任何编码。
对于这种复杂的URL,建议使用URLComponents/URLQueryItem创建它。URLComponents的好处是它可以代表您处理编码

let structure = """
{"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}
"""

var components = URLComponents(string: "https://api.coronavirus.data.gov.uk")!
components.path = "/v1/data"
components.queryItems = [URLQueryItem(name: "filters", value: "areaType=nation;areaName=england"),
                         URLQueryItem(name: "structure", value: structure)]

if let url = components.url {
    print(url)
}

旁注:

    • 从不**在JSONDecoder catch块中打印error.localizedDescription。它只向您显示无意义的一般错误消息。始终为print(error)
ghhkc1vu

ghhkc1vu3#

在getData方法中进行了更改,请尝试一次

private func getData(from url: String) {
    
    guard let validUrl  = URL(string: url) else {
        return
    }
    let getfromurl = URLSession.shared.dataTask(with: validUrl, completionHandler: {data, response, error in
        guard let data = data, error == nil else{
            print("Something Went Wrong")
            return
        }
        
        //Have data
        var result: Response?
        do {
            result = try JSONDecoder().decode(Response.self, from: data)
        }
        catch{
            print("failed to convert \(error.localizedDescription)")
        }
        
        guard let json = result else {
            return
        }
        
        print(json.data.date)
    })
    getfromurl.resume()

}

相关问题