Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. struct Photo: Codable {
  2.  
  3. let hasShadow: Bool
  4. let image: UIImage?
  5.  
  6. enum CodingKeys: String, CodingKey {
  7. case `self`, hasShadow, image
  8. }
  9.  
  10. init(hasShadow: Bool, image: UIImage?) {
  11. self.hasShadow = hasShadow
  12. self.image = image
  13. }
  14.  
  15. init(from decoder: Decoder) throws {
  16. let container = try decoder.container(keyedBy: CodingKeys.self)
  17. hasShadow = try container.decode(Bool.self, forKey: .hasShadow)
  18.  
  19. // This fails
  20. image = try container.decode(UIImage?.self, forKey: .image)
  21. }
  22.  
  23. func encode(to encoder: Encoder) throws {
  24. var container = encoder.container(keyedBy: CodingKeys.self)
  25. try container.encode(hasShadow, forKey: .hasShadow)
  26.  
  27. // This also fails
  28. try container.encode(image, forKey: .image)
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement