为什么这不起作用? Swift教程

时间:2014-06-10 02:39:05

标签: swift

对结构进行练习,我很困惑甲板为什么不返回一系列卡片? 我对C-ish语言相当陌生,但总体上对面向对象语言很有经验。

struct Card {
    var rank: Rank
    var suit: Suit
    func simpleDescription() -> String {
        return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
        }
    func buildDeck() -> Card[] {
        var range = 0..52
        var deck: Card[] = []
        var suits: Suit[] = [.Hearts, .Spades, .Diamonds, .Clubs]
        for i in range {
            for suit in suits {
                var card = Card(rank: Rank.fromRaw(i)!, suit: suit)
                deck.append(card)
            }
        }
        return deck
    }
}
let threeOfSpades = Card(rank: .Three, suit: .Spades)
let threeOfSpadesDescription = threeOfSpades.simpleDescription()

var deck = threeOfSpades.buildDeck()
deck

1 个答案:

答案 0 :(得分:0)

我怀疑的原因之一是您正在使用的半闭区域语法:

var range = 0..52 

应该是

var range = 0..<52 

因为Xcode得到了更新。这已经改变,但苹果公司也没有在其Swift书中改变它。