Leo Gaunt
Leo Gaunt

Reputation: 693

Unexpectedly found nil while unwrapping an Optional value even though it has a value assigned

I am using Swift to get a JSON from a Coronavirus API. However when I try to run the code I get this error.

Fatal error: Unexpectedly found nil while unwrapping an Optional value: line 22

My part of my code is

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()
    
    }

Line 22 is:

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

I am confused because I think that it means that url has nothing assigned to it, but even the debugger thinks it does.

UPDATE:

I can get the data but I get an error once I get it. The error is:

failed to convert valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "newDeathsByDeathDate", intValue: nil)], debugDescription: "Expected Int value but found null instead.", underlyingError: nil))

The failed to convert shows it is an error in decoding the JSON and the values.

Upvotes: 0

Views: 722

Answers (3)

vadian
vadian

Reputation: 285082

The URL is not encoded properly. A browser has its own way to encode an URL but URL(string: does not do any encoding at all.

For this kind of complex URL it's recommended to create it with URLComponents/URLQueryItem. The benefit of URLComponents is it handles the encoding on your behalf

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)
}

Side note:

Never print error.localizedDescription in a JSONDecoder catch block. It shows you only a meaningless generic error message. Always print(error)

Upvotes: 2

Jagveer Singh
Jagveer Singh

Reputation: 2328

made changes in getData method , please try this once

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()

}

Upvotes: 0

Andreas Oetjen
Andreas Oetjen

Reputation: 10199

The exception does not mean that url is nil, but URL(string:url) is nil

You need to check if the url string is a valid 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
       /* ... */
    }
}

Update

Since the url string is now given: The problem are the curly braces; they are marked as unsafe in RFC1738 and should be replaced by %7b (opening) and %7d (closing), hence:

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)

This is also an example in the 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'

Upvotes: 5

Related Questions