Swift枚举顺序和比较 [英] Swift Enumeration order and comparison

查看:126
本文介绍了Swift枚举顺序和比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到/了解有关如何通过Swift枚举的定义顺序比较枚举的文档。具体来说,当我创建枚举时,例如

I've had trouble finding/understanding documentation on how to compare enums in Swift by their order of definition. Specifically when I create an enumeration such as

enum EnumType {
    case First,  Second, Third
}

Swift不允许我直接按顺序比较枚举,例如

Swift does not allow me to directly compare enums by order, such as

let type1 = EnumType.First
let type2 = EnumType.Second
if type1 < type2 {println("good")} // error

它会生成编译错误无法调用' <'的参数列表类型为{EnumType,EnumType}。因此,我发现的唯一解决方案是将自己的比较运算符编写为重载,例如

it generates the compile error "cannot invoke '<' with argument list of of type {EnumType, EnumType}. So the only solution I've found is to write my own comparison operators as overloads, such as

enum EnumType : Int {
    case First = 0, Second, Third
}

func <(a: EnumType, b: EnumType) -> Bool {
    return a.rawValue < b.rawValue
}

let type1 = EnumType.First
let type2 = EnumType.Second
if type1 < type2 {println("good")} // Returns "good"

对于在我的应用程序中有很多用途和价值的重量级枚举来说,这一切都很好,但对于我可能要动态定义的轻量级枚举来说,使我可能想使用的所有运算符重载似乎负担过重。将单个小模块的一些常数排序。

This is all well and good for "heavy weight" enums that have a lot of use and value in my application, but overloading all the operators I might want to use seems excessively burdensome for 'lightweight" enums that I might define on the fly to bring order to some constants for a single small module.

有没有办法为我在项目中定义的每个枚举类型编写大量样板重载代码的方法?更好的是,Swift自动为没有关联类型的简单枚举提供比较运算符,这是我所缺少的吗?没有输入或输入为Int的? Swift知道如何比较Ints,所以为什么不能比较枚举Ints?

Is there way to do this without writing lots of boilerplate overloading code for every enum type I define in my project? Even better, is there something I'm missing to make Swift automatically provide comparison operators for simple enums that don't have associated types, ie. that are untyped or typed as Int? Swift knows how to compare Ints, so why can't it compare enum Ints?

推荐答案

只要您给枚举一个基础类型,它将符合协议 RawRepresentable

So long as you give your enum an underlying type, it’ll conform to the protocol RawRepresentable.

这意味着您可以为原始可表示的任何类型,并且具有可比较的原始类型,例如:

This means you can write a generic comparison operator for any type that is raw representable, and has a raw type that is comparable, like so:

func <<T: RawRepresentable where T.RawValue: Comparable>(a: T, b: T) -> Bool {
    return a.rawValue < b.rawValue
}

这意味着您的枚举将自动具有< 运算符:

which will mean your enum will automatically have a < operator:

enum E: Int {  // this would work with Double and String also
    // btw, no need to give a seed value of 0,
    // that happens automatically for Ints
    case A, B, C, D, E
}

E.A < E.C  // returns true

唯一要做的就是标记枚举作为可比较的,如果您想将其与需要以下条件的通用算法一起使用:

The only bit of boilerplate you’ll still have to do is tag your enum as Comparable in case you want to use it with generic algorithms that require that:

extension E: Comparable { }
// (no need for anything else - requirements are already fulfilled)

let a: [E] = [.C, .E, .A]
let b = sorted(a)
// b will now be [.A, .C, .E]

使其符合可比较也会使其具有< = > c和运算符会自动(由标准库提供)。

Making it conform to Comparable will also give it <=, >, and >= operators automatically (supplied by the standard library).

这篇关于Swift枚举顺序和比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆