Swift编程语言枚举实验的说明 [英] Explanation of The Swift Programming Language Enumerations Experiment

查看:107
本文介绍了Swift编程语言枚举实验的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究同一主题时,我首先遇到这个问题:
Swift编程语言枚举实验

I first stumbled across this question while researching the same topic: The Swift Programming Language Enumerations Experiment

我正在研究这个问题,但我的问题是,没有一个贡献者真正解释了他们的答案如何操纵数据的机制枚举。而且,最高的答案似乎是过时的,错误的等等。也许我错过了一些东西,因为我正在学习这种语言。如果是这样,有人可以解释我所缺少的东西,或者提供更多的细节的替代答案?

I am researching this, but my problem is that none of the contributors truly explain the mechanics of how their answers manipulate the data from the enumeration. Also, the top answer seems to be outdated, wrong, etc. Perhaps I am missing something because I am just learning this language. If so, can someone explain what I am missing or offer an alternative answer with more detail?

推荐答案

enum Rank: Int {
    case Ace = 1
    case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
    case Jack, Queen, King
    func simpleDescription() -> String {
        switch self {
        case .Ace:
            return "Ace"
        case .Jack:
            return "Jack"
        case .Queen:
            return "Queen"
        case .King:
            return "King"
        default:
            return String(self.toRaw())
        }
    }
}

这是问题中定义的枚举。在查看定义后,您可以看到其原始值将为整数。原始值有两个要求

This is the enumeration defined in the question. Upon looking at the definition, you can see its raw value will be an integer. There are two requirements for the raw values

1)原始值可以是字符串,字符或任何整数或浮点数类型。

1) Raw values can be strings, characters, or any of the integer or floating-point number types.

2)每个原始值在其枚举声明中必须是唯一的。

2) Each raw value must be unique within its enumeration declaration.

在示例中,您可以看到枚举情况 Ace 的原始值为1,符合在枚举定义中定义的类型。由于其余情况下没有定义原始值,所以Swift将智能地分配一个原始值,每个案例比前一个值多一个。在这种情况下,由于第一种情况明确表示为1,其后的情况为2,3等。
如果第一种情况没有明确说明,它将被赋值为0,以下情况将全部递增原始值。

In the example, you can see the enumeration case of Ace is assigned a raw value of 1 which conforms to the type defined in the enum definition. Since the rest of the cases do not have a raw value defined, Swift will intelligantly assign a raw value, having each case one more than the previous one. In this case, since the first case was explicitly stated with 1, the subsequent cases will be 2,3,etc. If the first case was not explicitly stated, it will be assigned a value of 0 and the the following cases will all increment that raw value.

当通过枚举你的程序,你可以使用他们的类型,而不是他们的原始值。

When passing enumerations around your programs, you can use their types rather than their raw values.

compareRanks(rankA: Rank, rankB: Rank)

枚举有一个名为 rawValue 的属性,返回与枚举情况相关联的原始值。因此,在您链接的代码示例中,使用此值可以比较两个枚举,以确定他们的排名是否相等。

Enumerations have a property called rawValue which will return the raw value associated with the enum case. Therefore in the code example you linked, using this value allows you to compare two enums to determine if their ranks are equal or not.

func compareRanks(rankA: Rank, rankB: Rank) -> Bool {
        return rankA.rawValue == rankB.rawValue
}

以前版本的Swift,有一个方法称为 toRaw(),返回原始值,但现在, rawValue 是一个将返回原始值的属性

In previous versions of Swift, there was a method called toRaw() which returned the raw value but now, rawValue is a property that will return the raw value

这篇关于Swift编程语言枚举实验的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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