简体   繁体   English

快速了解Equatable 3?

[英]Understanding Equatable in swift 3?

I have an array of enum which I need to delete one of the Enum. 我有一个枚举数组,需要删除一个枚举。 I have written generic remove operation based on here . 我已经基于此处编写了通用删除操作。 We usually conform Equatable to Struct. 我们通常使Equatable符合Struct。 So I am curious how Equatable will behave in Enum. 所以我很好奇Equatable在Enum中的表现。 If It is not a right way to write generic remove operation for collection of Enum array, then what will be the right one? 如果这不是编写用于收集Enum数组的泛型删除操作的正确方法,那么正确的方法是什么?

enum APIEndPoints: Equatable {
   case image
   case email
   case others
}

By definition, two enums of the same type are equal if they are of the same case. 根据定义,如果类型相同,则两个相同类型的枚举相等。 Thus .image is equal to .image , for example. 因此,例如, .image等于.image That is why you are able to test enum instances against one another using the == operator to find out what case something is. 这就是为什么您可以使用==运算符相互比较枚举实例,以找出什么情况。 Indeed, this fact is sort of the point of enums. 事实上,这其实是有点枚举的

(This, by the way, is also why enums cannot have stored properties. Two .image instances with different stored values for the same property would not be equal. And that would completely defeat the purpose of enums.) (顺便说一下,这也是为什么枚举不能具有存储属性的原因。两个具有相同属性的存储值的.image实例将是相等的。这将完全.image枚举的目的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM