Advertisement
Larme

Untitled

Jan 22nd, 2021
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.66 KB | None | 0 0
  1. enum MyEnum {
  2.     case one
  3.     case two
  4.     case three(user: String)
  5.  
  6.     func isOfSameType(_ other: MyEnum) -> Bool {
  7.         switch (self, other) {
  8.         case (.one, .one):
  9.             return true
  10.         case (.two, .two):
  11.             return true
  12.         case (.three, .three):
  13.             return true
  14.         default:
  15.             return false
  16.         }
  17.     }
  18. }
  19.  
  20. var array: [MyEnum] = []
  21. func add(_ value: MyEnum) {
  22.     if array.contains(where: { value.isOfSameType($0) }) { return }
  23.     array.append(value)
  24. }
  25.  
  26. add(.one)
  27. print(array)
  28. add(.two)
  29. print(array)
  30. add(.three(user: "Carl"))
  31. print(array)
  32. add(.three(user: "Junior"))
  33. print(array)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement