how to return string in Ganeric escaping colosure?
naveedmcs opened this issue · comments
Muhammad Naveed commented
struct HttpHandler
{
func getApiData<T:Decodable>(requestUrl: URL, resultType: T.Type, completionHandler:@escaping(_ result: T?, _ error: String?)-> Void)
{
URLSession.shared.dataTask(with: requestUrl) { (responseData, httpUrlResponse, error) in
if(error == nil && responseData != nil && responseData?.count != 0)
{
//parse the responseData here
let decoder = JSONDecoder()
do {
let result = try decoder.decode(T.self, from: responseData!)
_=completionHandler(result, nil)
}
catch let error{
debugPrint("error occured while decoding = \(error.localizedDescription)")
completionHandler(nil, "error occured while decoding = \(error.localizedDescription)")
}
}
}.resume()
}
}
Codecat15 commented
I didn't check this one out sorry for the late response. I think rather returning a string it's a good idea to return an object as we did in our API series, over there we can make use of an object than a single string. I will close this issue for now, but then if you have more questions or if you have faced any challenges then please feel free to re-open this