我正在使用for循环来遍历字符串数组。对于每个字符串,我为它找到一个新值。找到新值后,我需要替换相应索引处的字符串。
var myStrings = [String]()
for var i = 0; i < myStrings.count; i++ {
let newValue: String = //Do some work to find the new value
myStrings[i] = newValue //This is what I thought would work, but I'm getting a crash and error saying that the array is out of index.
}
错误表明数组已超出索引范围。
答案 0 :(得分:0)
想出来。我找到newValue的工作涉及使用一些闭包。我不得不在封闭内部将i
重新定义为index
,以便能够在封闭内使用它。