private func createWeatherObjectWith(json: Data, x:Any.Type ,completion: @escaping (_ data: Any?, _ error: Error?) -> Void) {
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let weather = try decoder.decode(x.self, from: json)
return completion(weather, nil)
} catch let error {
print("Error creating current weather from JSON because: \(error.localizedDescription)")
return completion(nil, error)
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我编写上面的代码,通过传递类类型将Json字符串解码为类对象.但是它给出了以下错误
Cannot invoke 'decode' with an argument list of type '(Any.Type, from: Data)'
Run Code Online (Sandbox Code Playgroud)