Swift Tour Card.createDeck()返回[{(枚举值),(枚举值)}]

时间:2015-08-09 02:27:21

标签: ios swift enums

我正在完成Apple Swift之旅,并且遇到了返回枚举值的问题。我的代码似乎运行正常,但是当我创建一副牌时,不会返回适当的等级和套装,我只是将[{(enum value), (enum value)}]返回52次。

当Swift第一次出现时,显然有一个错误导致了这个问题,但不久之前它已经修复了Xcode 6.3我相信,(我正在运行Xcode 6.4),所以我不明白这是怎么回事。

我已经能够在某些情况下通过使用.simpleDescription来返回实际字符​​串来解决问题,但我似乎无法在此实例中使用它。

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.rawValue)
        }
    }
}

enum Suit  {
    case Spades, Hearts, Diamonds, Clubs
    func simpleDescription() -> String {
        switch self {
        case .Spades:
            return "spades"
        case .Hearts:
            return "hearts"
        case .Diamonds:
            return "diamonds"
        case .Clubs:
            return "clubs"
        }
    }
    static func color(suitName: Suit) -> String {
        if(suitName.simpleDescription() == "spades" || suitName.simpleDescription() == "clubs") {
            return "Black"
        } else {
            return "Red"
        }
    }
}

struct Card {
    var rank: Rank
    var suit: Suit
    func simpleDescription() -> String {
        return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
    }
    static func createDeck() -> [Card] {
        let ranks = [Rank.Ace, Rank.Two, Rank.Three, Rank.Four, Rank.Five, Rank.Six, Rank.Seven, Rank.Eight, Rank.Nine, Rank.Ten, Rank.Jack, Rank.Queen, Rank.King]
        let suits = [Suit.Spades, Suit.Hearts, Suit.Diamonds, Suit.Clubs]
        var deck = [Card]()
        for suit in suits {
            for rank in ranks {
                deck.append(Card(rank: rank, suit: suit))
            }
        }
        println(deck)
        return deck
    }
}
let threeOfSpades = Card(rank: .Three, suit: .Spades)
let threeOfSpadesDescription = threeOfSpades.simpleDescription() // This code returns the card properly

Card.createDeck()

1 个答案:

答案 0 :(得分:2)

就是这样。 println未显示枚举值。实现Printable(和description),或更新到修复它的Swift 2.0。