Swift编程语言枚举实验 [英] The Swift Programming Language Enumerations Experiment

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

问题描述

我正在通过Swift编程语言一书,但我坚持一个实验。



我被赋予了这个代码:

 枚举排名:Int {
case Ace = 1
case二,三,四,五,六,七,八,九,十
案例杰克,女王,国王
func simpleDescription() - > String {
switch self {
case .Ace:
returnAce
case .Jack:
returnJack
case .Queen:
returnQueen
case .King:
returnKing
default:
return String(self.toRaw())
}
}
}

对于实验,我必须编写一个比较两个通过比较原始值来分级值。



我已经走了:

  func rankCompare(first:String,second:String) - > String {
let firstRank = Rank.first
}

但是,由于我不知道如何传递枚举值,我最终错误了。



有人可以帮助吗? / p>

解决方案

枚举值可以像其他类型一样传递,以下函数是 Rank 枚举,并比较一个等级另一个。

  func compareToOther(other:Rank) - > Bool {//其他类型为Rank 
return self.toRaw()== other.toRaw()
}

这是快速实现和使用的截图。


I'm making my way through The Swift Programming Language book, but I'm stuck on an experiment.

I'm given this code:

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())
        }
    }
}

For the experiment, I have to "Write a function that compares two Rank values by comparing their raw values.

I had a go:

func rankCompare(first: String, second: String) -> String {
    let firstRank = Rank.first
}

But I ended up with errors because I don't know how to pass Enum values.

Can someone help?

解决方案

Enum values can be passed just like other types. The following function is part of the Rank enum and compares one Rank to another.

func compareToOther(other:Rank) -> Bool {    // other is of type Rank 
        return self.toRaw() == other.toRaw()
}

Here is a screenshot of the quick implementation and usage.

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

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