简体   繁体   中英

The Swift Programming Language Enumerations Experiment

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. 在此处输入图片说明

You can pass enums by just passing the enum name:

// someRank is a Rank enum value
func myFunction (someRank: Rank) -> () {

}

And then you can just call it:

myFunction(Rank.Ace)

I am also a beginner, but this is how I worked throughout the experiment. First I added this;

func compareTwoCards(card1: Rank, card2: Rank) -> String {
if card1.toRaw() == card2.toRaw() {
    return "Cards are equal"
} else {
    if card1.toRaw() > card2.toRaw() {
        return "Card1 is greater"
    } else {
        return "Card2 is greater"
    }
} }

Then I created two Rank objects

let ace = Rank.Ace
let queen = Rank.Queen

Finally, I called it three different ways to test it;

compareTwoCardsTake2(ace, queen)
compareTwoCardsTake2(queen, ace)
compareTwoCardsTake2(ace, ace)

Can some one with more experience please reply if there is a better/more elegant way of performing the compare?

I solved it like this:

func rankCompare(first: Rank, second: Rank) -> String {
    if(first.rawValue > second.rawValue) {
        return "\(first.simpleDescription()) beats \(second.simpleDescription())."
    }
    else if second.rawValue > first.rawValue {
        return "\(second.simpleDescription()) beats \(first.simpleDescription())."
    }
    else {
        return "\(first.simpleDescription()) equals \(second.simpleDescription())."
    }
}

let king = Rank.King
let queen = Rank.Queen
let seven = Rank.Seven
rankCompare(king, queen)
rankCompare(seven, king)
rankCompare(queen, queen)

Use .rawValue for doing the comparisons and .simpleDescription() for writing out your answer.

This code can be used to determine if two enumeration values are equal or not. .toRaw() is obsolete, so .rawValue must be used to obtain the raw value for comparison. An edited version of this function (to make a full comparison with type string information, not just "true" or "false") should be used to complete the exercise. Hint For Editing: this function is of type Bool.

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

To see the code and contributors that made this answer possible, please see the question: Explanation of The Swift Programming Language Enumerations Experiment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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