gpt4 book ai didi

string - 在 Swift 中改组字符串 - 不支持交换位置

转载 作者:行者123 更新时间:2023-11-28 13:02:24 24 4
gpt4 key购买 nike

<分区>

我有这段代码可以快速打乱一个字符串。出于某种原因,它在 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'

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com