December 2018
Intermediate to advanced
414 pages
10h 19m
English
A Decodable object is an object that can be transformed from Data to a pure Swift object or struct, through Decoder. This section will not provide a full coverage of codables, but rather, a quick example that shows how you can leverage this when communicating with a server in a generic way.
Let's re-work the previous example a bit and improve the code; instead of returning data, we want to return a pure Swift object:
func get<T>(url: URL, callback: @escaping (T?, Error?) -> Void) -> URLSessionTask where T: Decodable { let task = URLSession.shared.dataTask(with: url) { data, response, error in if let error = error { callback(nil, error) return } // Very simple handling of errors, you may wanna // have a more ...
Read now
Unlock full access