@H_404_8@
@H_404_8@
enum AnEnumeration { case aSimpleCase case anotherSimpleCase case aMoreComplexCase(String) } let a1 = AnEnumeration.aSimpleCase let b1 = AnEnumeration.aSimpleCase a1 == b1 // Should be true. let a2 = AnEnumeration.aSimpleCase let b2 = AnEnumeration.anotherSimpleCase a2 == b2 // Should be false. let a3 = AnEnumeration.aMoreComplexCase("Hello") let b3 = AnEnumeration.aMoreComplexCase("Hello") a3 == b3 // Should be true. let a4 = AnEnumeration.aMoreComplexCase("Hello") let b4 = AnEnumeration.aMoreComplexCase("World") a3 == b3 // Should be false.
@H_404_8@
error: MyPlayground.playground:7:4: error: binary operator '==' cAnnot be applied to two 'AnEnumeration' operands a1 == b1 // Should be true. ~~ ^ ~~ MyPlayground.playground:7:4: note: binary operator '==' cAnnot be synthesized for enums with associated values a1 == b1 // Should be true. ~~ ^ ~~
翻译:如果您的枚举使用关联值,则无法测试它的等效性.@H_404_8@
注意:如果删除了.aMoreComplexCase(和相应的测试),那么代码将按预期工作.@H_404_8@
看起来过去人们已经决定使用运算符重载来解决这个问题:How to test equality of Swift enums with associated values.但是现在我们有了Swift 4,我想知道是否有更好的方法?或者,如果有更改使链接的解决方案无效?@H_404_8@
谢谢!@H_404_8@
@H_404_8@
> SE-0185 Synthesizing Equatable and Hashable conformance@H_404_8@
已被Swift 4.1(Xcode 9.3)接受并实现:@H_404_8@
@H_404_8@
因此,它就足够了@H_404_8@
@H_404_8@
在你的例子中 – 因为String是Equatable – 它足以声明@H_404_8@
@H_404_8@
enum AnEnumeration: Equatable { case aSimpleCase case anotherSimpleCase case aMoreComplexCase(String) }
以上是大佬教程为你收集整理的如何在Swift 4中测试具有关联值的枚举情况的等效性全部内容,希望文章能够帮你解决如何在Swift 4中测试具有关联值的枚举情况的等效性所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。