Created
February 9, 2017 20:44
-
-
Save JohannMG/db8fb9a45051034190e6c02298ad63cf to your computer and use it in GitHub Desktop.
Response object representing the response from the server. Quick JSON parsing so you stop repeating the snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Response object representing the response from the server. | |
| Core: Data (NSData), Response (NSURLResponse), Error (NSError) | |
| Convienece: | |
| HTTP | |
| JSON (Meant for an individual object) | |
| JSON Array (Meant for parsing an entire list of objects) | |
| */ | |
| struct Response { | |
| let data: Data! | |
| let response: URLResponse! | |
| var error: NSError? | |
| var responseHTTP: HTTPURLResponse! { | |
| return response as? HTTPURLResponse | |
| } | |
| var responseJSON: [String: AnyObject]? { | |
| if let data = data { | |
| do { | |
| return try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] | |
| } catch { | |
| return nil | |
| } | |
| } | |
| return nil | |
| } | |
| var responseJSONArray: [AnyObject]? { | |
| if let data = data { | |
| do { | |
| return try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [AnyObject] | |
| } catch { | |
| return nil | |
| } | |
| } | |
| return nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment