我在使用这个 drawRandomCard 函数时遇到了问题。
它可以正常工作一段时间,但最终它会使应用程序崩溃。
这是代码:
import Foundation
var cardDeck = Array<PlayingCard>()
class Deck {
func addCard(card : PlayingCard , atTop : Bool = false){
if atTop {
cardDeck.insert(card, atIndex: 0);
}else{
cardDeck += card
}
}
func drawRandomCard() -> PlayingCard{
var card = PlayingCard()
var randomNumber : Int = Int(arc4random()) % (cardDeck.count - 1)
card = cardDeck[randomNumber]
cardDeck.removeAtIndex(randomNumber)
return card
}
}