【发布时间】:2015-12-02 04:45:20
【问题描述】:
我的卡片由 52 张卡片组成,所以我为它们创建了一个array,当用户点击屏幕时,就会显示一张卡片。
我希望当一张卡片被拉出来以后,它就不能再被拉出来了。我使用removeAtIndex 将拉出的卡从array 中移除。当用户在任意位置触摸屏幕时,函数pullCard被调用:
// Cards
let card1 = (1, "AS", "RULE 1")
let card2 = (2, "2", "RULE 2")
let card3 = (3, "3", "RULE 3")
let card4 = (4, "4", "RULE 4")
let card5... (to 52)
// Array
var cards = [
card1,
card2,
card3,
card4,
card5... (to 52)
]
func pullCard() {
let randomCard = Int(arc4random_uniform(UInt32(cards.count)))
let cardsIndex = randomCard - 1
let card = cards[cardsIndex]
cards.removeAtIndex(cardsIndex)
}
例如,游戏拉出32张卡片,所以它从数组中删除它,如果游戏拉出同一张卡片,它会因为卡片被删除而崩溃。
我尝试使用 if 语句:
if cards[cardsIndex] == nil {
pullCard()
}
但它不起作用。 请帮帮我:)
【问题讨论】:
-
你的意思是你的if语句是
cards[cardsIndex] != nil?
标签: ios arrays swift random swift2