У меня есть API, который дает мне два разных ответа.
здесь, когда данные верны, и у меня есть объекты:
{ "latlng": [My Objects] }
, а вот когда мои данные пусты:
[]
поэтому мой вопрос заключается в том, как обрабатывать этот пустой ответ, когда я использую jsonDecoder?
моя сетевая функция:
func performNetworkingTaskWithParams<T: Codable>(endpoint: EndPointType, type: T.Type, params: [String : String],completion: ((_ response: T)-> Void)?) {
let url = endpoint.baseURL.appendingPathComponent(endpoint.path)
let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = [
"Accept" : "application/json",
"Content-Type" : "application/x-www-form-urlencoded"
]
var urlRequest = URLRequest(url: url)
let session = URLSession(configuration: config)
urlRequest.encodeParameters(parameters: params)
let taskSession = session.dataTask(with: urlRequest) { (data, response, error) in
if let myError = error {
print("request error:\(myError)")
return
}
guard let responseData = data else {
print("response is null!")
return
}
let response = Response(data: responseData)
guard let decoded = response.decode(type) else {
print("cannot decode data!")
return
}
completion?(decoded)
}
taskSession.resume()
}