Guest User

Untitled

a guest
Jan 4th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. final class Post: Model, Codable {
  2. // other stuff...
  3.  
  4. // MARK: Codable compliance
  5. private enum CodingKeys: String, CodingKey {
  6. case id
  7. case content
  8. }
  9.  
  10. func encode(to encoder: Encoder) throws {
  11. var container = encoder.container(keyedBy: CodingKeys.self)
  12. // Note that Identifier is not Codable, so as a workaround, I'm always representing it as a String.
  13. try container.encode(id?.string, forKey: .id)
  14. try container.encode(content, forKey: .content)
  15. }
  16.  
  17. init(from decoder: Decoder) throws {
  18. let container = try decoder.container(keyedBy: CodingKeys.self)
  19. content = try container.decode(String.self, forKey: .content)
  20. }
  21. }
  22.  
  23. // You can now use request.decodeJSONBody(Post.self), post.makeResponse(), etc.
Add Comment
Please, Sign In to add comment