在Swift中使用特殊字符解码URL

问题描述

我使用的API提供了URL链接,其中可以包含特殊字符,例如“http://es.dbpedia.org/resource/Análisis_de_datos”(内部字母“ á”)。 / p>

这是绝对有效的URL,但是,如果可解码类包含可选的URL?变量,则无法对其进行解码。

我可以在课堂上将URL?更改为String?,并使用一个类似URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)的计算变量,但是也许有一个更优雅的解决方案。

要在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 

解决方法

有多种方法可以解决此问题,但我认为使用属性包装器可能是最优雅的方法:

@propertyWrapper
struct URLPercentEncoding {
   var wrappedValue: URL
}

extension URLPercentEncoding: Decodable {
   public init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()
        
      if let str = try? container.decode(String.self),let encoded = str.addingPercentEncoding(
                              withAllowedCharacters: .urlFragmentAllowed),let url = URL(string: encoded) {

         self.wrappedValue = url

      } else {
         throw DecodingError.dataCorrupted(
            .init(codingPath: container.codingPath,debugDescription: "Corrupted url"))
      }
   }
}

那么您可以像这样使用它,而该模型的使用者不必了解任何有关它的信息:

struct Response: Decodable {
    @URLPercentEncoding let url: URL
}
,

您可以扩展KeyedDecodingContainer并实现自己的URL解码方法:

extension KeyedDecodingContainer {
    func decode(_ type: URL.Type,forKey key: K) throws -> URL {
        let string = try decode(String.self,forKey: key)
        guard let url = URL(string: string.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) ?? "")
        else {
            throw DecodingError.dataCorrupted(.init(codingPath: codingPath,debugDescription: "The stringvalue for the key \(key) couldn't be converted into a URL value: \(string)"))
        }
        return url
    }
    // decoding an optional URL
    func decodeIfPresent(_ type: URL.Type,forKey key: K) throws -> URL? {
        try URL(string: decode(String.self,forKey: key).addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) ?? "")
    }
}

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

do {
    let encodedData = try encoder.encode(container)
    print(String(data: encodedData,encoding: .utf8))
    let decoder = JSONDecoder()
    let response = try decoder.decode(Response.self,from: encodedData)
    print(response)
} catch {
    print(error)
}

这将打印:

Optional(“ {” url“:” http:\ / \ / es.dbpedia.org \ / resource \ /Análisis_de_datos“}”)

响应(URL:http://es.dbpedia.org/resource/An%C3%A1lisis_de_datos