- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 347字
- 2021-07-02 14:45:12
Parsing responses with Decodable
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 in depth implementation
if let data = data {
do {
let result = try JSONDecoder().decode(T.self, from: data)
callback(result, nil)
} catch let error {
callback(nil, error)
}
} else {
callback(nil, nil)
}
}
task.resume()
return task
}
The preceding get method will fetch the contents of a URL, and then try to parse the received data into the expected type. You can use it the following way:
struct MyResult: Decodable {
/*
you can put anything decodable here
the compiler will generate the decodable implementation
*/
}
func handle(result: MyResult) {
print(result)
}
func handle(handle: Error) {
print(handle)
}
_ = get(url: URL(string: "http://example.com")!) { (result: MyResult?, error) in
switch (result, error) {
case (.some(let result), _):
handle(result: result)
case (_, .some(let error)):
handle(handle: error)
default: // both are nil :/
break;
}
}
In the preceding example, we'll get the contents of http://example.com, and the get<T> function will automatically try to map the contents of the response body into the expected MyResult. This gives you a compile time guarantee that only valid response bodies will be processed further down your application, which is very interesting when building robust clients.
Now that we have implemented a simple get method and a parser, let's look at how we can tie the loop with another method.
- 從零開始學Hadoop大數據分析(視頻教學版)
- Google Visualization API Essentials
- 信息系統與數據科學
- Voice Application Development for Android
- Visual Studio 2015 Cookbook(Second Edition)
- 工業大數據分析算法實戰
- Hadoop大數據實戰權威指南(第2版)
- Oracle 12c云數據庫備份與恢復技術
- 深入淺出Greenplum分布式數據庫:原理、架構和代碼分析
- Oracle數據庫管理、開發與實踐
- openGauss數據庫核心技術
- 數據中臺實戰:手把手教你搭建數據中臺
- 領域驅動設計精粹
- Learning Ansible
- MySQL核心技術手冊