Created
November 5, 2017 14:17
-
-
Save pgpt10/5a6289d87c8f7ab5c6881ec3d178017c to your computer and use it in GitHub Desktop.
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
struct Photo | |
{ | |
var title: String | |
var size: Size | |
enum CodingKeys: String, CodingKey | |
{ | |
case title = "name" | |
case width | |
case height | |
} | |
} | |
extension Photo: Encodable | |
{ | |
func encode(to encoder: Encoder) throws | |
{ | |
var container = encoder.container(keyedBy: CodingKeys.self) | |
try container.encode(title, forKey: .title) | |
try container.encode(size.width, forKey: .width) | |
try container.encode(size.height, forKey: .height) | |
} | |
} | |
extension Photo: Decodable | |
{ | |
init(from decoder: Decoder) throws | |
{ | |
let values = try decoder.container(keyedBy: CodingKeys.self) | |
title = try values.decode(String.self, forKey: .title) | |
let width = try values.decode(Double.self, forKey: .width) | |
let height = try values.decode(Double.self, forKey: .height) | |
size = Size(width: width, height: height) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment