Reputation: 8951
I am completing the Apple Swift tour, and am running into trouble with return enum values. My code seems to be running fine, but when I create a deck of cards, the appropriate rank and suit are not returned, I just get [{(enum value), (enum value)}]
returned 52 times.
Apparently there was a bug causing this when Swift first came out, but it was fixed a while ago with Xcode 6.3 I believe, (I am running Xcode 6.4), so I don't see how that could be the issue.
I've been able to resolve the problem in some circumstances by using .simpleDescription
to return the actual string but I can't seem to get that to work in this instance.
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()
Upvotes: 0
Views: 204
Reputation: 535118
That's just the way it is. println
doesn't show enum values. Implement Printable (and description
), or update to Swift 2.0, which fixes it.
Upvotes: 2