How may I test the equivalency of enumeration cases with associated values in Swift 4

前端 未结 1 743
抹茶落季
抹茶落季 2020-12-07 02:14

I would like to test the equivalency of a couple variables of enumeration types, like this:

enum AnEnumeration {
          


        
相关标签:
1条回答
  • 2020-12-07 03:02

    The Swift proposal

    • SE-0185 Synthesizing Equatable and Hashable conformance

    has been accepted and implemented in Swift 4.1 (Xcode 9.3):

    ... synthesize conformance to Equatable/Hashable if all of its members are Equatable/Hashable.

    therefore it suffices to

    ... opt-in to automatic synthesis by declaring their type as Equatable or Hashable without implementing any of their requirements.

    In your example – since String is Equatable – it will suffice to declare

    enum AnEnumeration: Equatable {
      case aSimpleCase
      case anotherSimpleCase
      case aMoreComplexCase(String)
    }
    

    and the compiler will synthesize a suitable == operator.

    0 讨论(0)
提交回复
热议问题