Skip to content

Instantly share code, notes, and snippets.

@pgpt10
Created November 5, 2017 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgpt10/5a6289d87c8f7ab5c6881ec3d178017c to your computer and use it in GitHub Desktop.
Save pgpt10/5a6289d87c8f7ab5c6881ec3d178017c to your computer and use it in GitHub Desktop.
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