Is it possible to loop through enum values in Swift? Or what is the alternative?
I'm working through Apple's Swift language guide, and I came across this example on enums.
// EXPERIMENT
//
// Add a method to Card that creates a full deck of cards,
// with one card of each combination of rank and suit.
struct Card {
var rank: Rank
var suit: Suit
func simpleDescription() -> String {
return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
}
}
let threeOfSpades = Card(rank: .Three, suit: .Spades)
let threeOfSpadesDescription = threeOfSpades.simpleDescription()
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"
}
}
}
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())
}
}
}
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
I've tried the following, but the docs say enums in Swift are not assigned underlying integer values like in C, so I'm probably barking up the wrong tree.
Is there a better way solve this problem?
func deck() -> Card[]{
var deck: Card[]
for s in Suit {
for r in Rank {
deck += Card(rank: r, suit: s)
}
}
return deck
}
func deck2() -> Card[]{
var deck: Card[]
for var s: Suit = .Spades; s <= .Clubs; s++ {
for var r: Rank = .Ace; r <= .King; r++ {
deck += Card(rank: r, suit: s)
}
}
return deck
}