Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. let others : [String: OthersType?]?
  2.  
  3.  
  4.  
  5. enum OthersType: Codable {
  6. case int(Int)
  7. case string(String)
  8. case bool(Bool)
  9.  
  10. init(from decoder: Decoder) throws {
  11. let container = try decoder.singleValueContainer()
  12.  
  13. do {
  14. self = try .int(container.decode(Int.self))
  15. } catch DecodingError.typeMismatch {
  16. do {
  17. self = try .string(container.decode(String.self))
  18. } catch DecodingError.typeMismatch {
  19. do {
  20. self = try .bool(container.decode(Bool.self))
  21. } catch DecodingError.typeMismatch {
  22. throw DecodingError.typeMismatch(OthersType.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Encoded payload not of an expected type"))
  23. }
  24. }
  25. }
  26. }
  27.  
  28. func encode(to encoder: Encoder) throws {
  29. var container = encoder.singleValueContainer()
  30. switch self {
  31. case .int(let int):
  32. try container.encode(int)
  33. case .string(let string):
  34. try container.encode(string)
  35. case .bool(let bool):
  36. try container.encode(bool)
  37. }
  38. }
  39.  
  40. func getValue(_ ofTheWord: OthersType) -> String {
  41. var result = String(describing: ofTheWord)
  42. if result.contains("string(") {
  43. result = result.replacingOccurrences(of: "string("", with: "")
  44. result.removeLast(2)
  45.  
  46. } else if result.contains("int(") {
  47. result = result.replacingOccurrences(of: "int(", with: "")
  48. result.removeLast(1)
  49.  
  50. } else if result.contains("bool(") {
  51. result = result.replacingOccurrences(of: "bool(", with: "")
  52. result.removeLast(1)
  53. }
  54. return result
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement