An API I work with provides URL links, that can contain special characters like "http://es.dbpedia.org/resource/Análisis_de_datos" (the letter "á" inside).
It's an absolutely valid URL, however, if a decodable class contains an optional URL? variable, it can't be decoded.
I can change URL? to String? in my class and have a computed variable with something like URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) but perhaps there is a more elegant solution.
To reproduce in Playground:
struct Container: Encodable {
let url: String
}
struct Response: Decodable {
let url: URL?
}
let container = Container(url: "http://es.dbpedia.org/resource/Análisis_de_datos")
let encoder = JSONEncoder()
let encodedData = try encoder.encode(container)
let decoder = JSONDecoder()
let response = try? decoder.decode(Response.self, from: encodedData)
// response == nil, as it can't be decoded.
let url = response?.url