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

痴心易碎 提交于 2019-11-28 02:00:32

The Swift proposal

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!