ios 在 Swift 中循环遍历枚举值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24061584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looping through enum values in Swift
提问by glaslong
Is it possible to loop through enum values in Swift? Or what is the alternative?
是否可以在 Swift 中循环遍历枚举值?或者有什么替代方案?
I'm working through Apple's Swift language guide, and I came across this example on enums.
我正在阅读 Apple 的 Swift 语言指南,并且在枚举上遇到了这个示例。
// 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 Hyman, Queen, King
func simpleDescription() -> String {
switch self {
case .Ace:
return "ace"
case .Hyman:
return "Hyman"
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
摘自:Apple Inc. “The Swift Programming Language”。电子书。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.
我已经尝试了以下方法,但是文档说 Swift 中的枚举没有像 C 中那样分配底层整数值,所以我可能在咆哮错误的树。
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
}
采纳答案by Erik
Is there another way? Sure. Is it better, that's for you to decide:
还有其他方法吗?当然。是否更好,由您决定:
func generateDeck() -> Card[]
{
let ranksPerSuit = 13
var deck = Card[]()
for index in 0..52
{
let suit = Suit.fromRaw(index / ranksPerSuit)
let rank = Rank.fromRaw(index % ranksPerSuit + 1)
let card = Card(rank: rank!, suit: suit!)
deck.append(card)
}
return deck
}
let deck = generateDeck()
for card : Card in deck { println("\(card.description)") }
To use this, you will need to make sure that Rank
and Suit
enums both use Int
for their type definitions (ex: enum Rank : Int
).
要使用它,您需要确保Rank
和Suit
枚举都Int
用于它们的类型定义(例如:)enum Rank : Int
。
Rank.Ace
should equal 1
and the first Suit
case should equal 0
.
Rank.Ace
应该等于1
和第一种Suit
情况应该相等0
。
If you want to loop similar to your existing code, you should still make your enums Int
types so you can use Rank.King.toRaw()
and the like.
如果你想循环类似于你现有的代码,你仍然应该制作你的枚举Int
类型,以便你可以使用等等Rank.King.toRaw()
。
The Apple documentation states that enums are not restricted to being 'simply integer values', but certainly can be if you desire them to be.
Apple 文档指出,枚举不限于“简单的整数值”,但如果您愿意,当然可以。
UPDATE
更新
Idea taken from comment by @jay-imerman, and applicable to Swift 5
来自@jay-imerman 的评论的想法,适用于 Swift 5
extension Rank: CaseIterable {}
extension Suit: CaseIterable {}
func generateDeck() -> [Card] {
var deck = [Card]();
Rank.allCases.forEach {
let rank = ##代码##
Suit.allCases.forEach {
let suit = ##代码##
deck.append(Card(rank: rank, suit: suit))
}
}
return deck;
}