Guest User

Untitled

a guest
Feb 16th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import UIKit
  2.  
  3. enum SomeEnum: String, ExpressibleByStringLiteral, Encodable {
  4. case option1
  5. case option2
  6. case option3
  7.  
  8. init(stringLiteral value: String) {
  9. switch value {
  10. case "option1": self = .option1
  11. case "option2": self = .option2
  12. case "option3": self = .option3
  13. default: fatalError("Case is not allowed")
  14. }
  15. }
  16.  
  17. init(extendedGraphemeClusterLiteral value: String) {
  18. self.init(stringLiteral: value)
  19. }
  20.  
  21. init(unicodeScalarLiteral value: String) {
  22. self.init(stringLiteral: value)
  23. }
  24.  
  25.  
  26. }
  27.  
  28. protocol RestrictedEnum {}
  29.  
  30. class Query1 {
  31.  
  32. enum Query1Enum: SomeEnum {
  33. case option2 = "option2"
  34. case option3 = "option3"
  35. }
  36.  
  37. init(_ options: Query1Enum...) {
  38.  
  39. }
  40. }
  41.  
  42. class Query2: Encodable {
  43.  
  44. enum Query2Enum: SomeEnum, Encodable {
  45. case option2 = "option2"
  46. case option3 = "option3"
  47.  
  48. func encode(to encoder: Encoder) throws {
  49. var c = encoder.singleValueContainer()
  50. try c.encode(self.rawValue)
  51. }
  52. }
  53.  
  54. let options: [Query2Enum]
  55. init(_ options: Query2Enum...) {
  56. self.options = options
  57. }
  58. }
  59.  
  60. let x = Query1(.option2)
  61. let y = Query2(.option2, .option3)
  62.  
  63. let zData = "option2".data(using: .utf8)
  64. //let z = try! JSONDecoder().decode(SomeEnum.self, from: zData!)
  65. let z1 = try! JSONEncoder().encode(Query2(.option2))
  66.  
  67. //print(z)
  68. print(String(data: z1, encoding: .utf8))
Add Comment
Please, Sign In to add comment