使用Swift array.count和arc4random()

时间:2014-07-21 05:30:24

标签: swift

为了让这段代码有效,我错过了什么? nodesLeft是[Int]。

let x = nodesLeft.count
let r = Int(arc4random_uniform(x))

我收到错误:

游乐场执行失败:错误:: 136:40:错误:' NSNumber'不是' UInt32'的子类型。         令r = Int(arc4random_uniform(x))

2 个答案:

答案 0 :(得分:7)

我遇到了与arc4random_uniform()相同的问题;它的参数必须是UInt32所以如此投射x

let x = UInt32(nodesLeft.count)
let r = Int(arc4random_uniform(x))

答案 1 :(得分:1)

必须在UInt32

中投射x
    var nodesLeft = ["T1", "T2", "T3", "T4"]

    let x = UInt32(nodesLeft.count)
    println(x)

    let r = Int(arc4random_uniform(x))
    println(r)