【发布时间】:2016-03-25 03:44:30
【问题描述】:
我正在使用此处的数组随机播放功能:http://iosdevelopertips.com/swift-code/swift-shuffle-array-type.html。
在这一行:
for var index = array.count - 1; index > 0; index -= 1
在下面的代码中
func shuffleArray<T>( arrayparam: Array<T>) -> Array<T>
{
var array = arrayparam
for var index = array.count - 1; index > 0; index -= 1
{
// Random int from 0 to index-1
let j = Int(arc4random_uniform(UInt32(index-1)))
// Swap two array elements
// Notice '&' required as swap uses 'inout' parameters
swap(&array[index], &array[j])
}
return array
}
Swift 抛出这个警告:
C 风格的 for 语句已被弃用,未来将被移除 斯威夫特版本
这里没有关于应该使用什么的建议。有什么想法可以代替它吗?
【问题讨论】:
-
for index in array.indices.reverse() { ... } -
这行当 index==0 时应用肯定会崩溃:let j = Int(arc4random_uniform(UInt32(index-1)))