Swift:无法交换数组中的元素

时间:2015-05-24 05:56:00

标签: xcode swift

G'day All,

我试图改变一副纸牌。我的算法是逐步通过套牌并用随机选择的卡交换每张卡。在这个阶段,我只是尝试将已知卡与随机选择的卡交换。即使我可以访问数组并检索每张卡,构建也会在交换行失败。

该功能如下:

    func shuffle() ->[String]{
    let deck = ["AS","2S","3S","4S","5S","6S","7S","8S","9S","10S","JS","QS","KS"]

    var i = 12
    var j = Int(arc4random_uniform(10))

    println("The card for \(i) is \(deck[i])")
    println("The card for \(j) is \(deck[j])")

    swap(&deck[i], &deck[j])

    return deck

}

错误是:

Cannot invoke 'subscript' with an argument list of type '(inout inout $T6, inout inout $T11)'

任何人都能解释一下吗?

的Ta,

1 个答案:

答案 0 :(得分:0)

对于Swift 4试试这个:

deck.swapAt(i, j)