我有这个代码在swift中随机播放一个字符串。出于某种原因,它在Xcode 7.1中给出了一个错误“不支持交换位置。我认为它工作正常。任何我错误的想法都非常赞赏!
let selectedWord = word1 // word1 is a string
var chars = Array(selectedWord.characters)
chars.shuffleString()
let shuffledWord = String(chars)
word1 = shuffledWord
extension Array {
mutating func shuffleString() {
for index in 0..<(count - 1) {
let j = Int(arc4random_uniform(UInt32(count - index))) + index
swap(&self[index], &self[j]) // error on this line saying 'swapping a location with itself is not supported'
答案 0 :(得分:2)
在最新版本的Xcode中更改了.state('main.home', {
url: '/home',
templateUrl: '/main/home/home.html',
controller: 'HomeCtrl',
onEnter: function ($state)
{
if (condition === true){
$timeout(function() {
$state.go('main.catch')
)};
}
}
})
函数,以防止变量与自身交换。您可以在交换之前添加swap
语句,以确保guard
和index
不相同:
j