为了让这段代码有效,我错过了什么? nodesLeft是[Int]。
let x = nodesLeft.count
let r = Int(arc4random_uniform(x))
我收到错误:
游乐场执行失败:错误:: 136:40:错误:' NSNumber'不是' UInt32'的子类型。 令r = Int(arc4random_uniform(x))
答案 0 :(得分:7)
我遇到了与arc4random_uniform()
相同的问题;它的参数必须是UInt32
所以如此投射x
:
let x = UInt32(nodesLeft.count)
let r = Int(arc4random_uniform(x))
答案 1 :(得分:1)
必须在UInt32
var nodesLeft = ["T1", "T2", "T3", "T4"]
let x = UInt32(nodesLeft.count)
println(x)
let r = Int(arc4random_uniform(x))
println(r)