我正在使用 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.
}
错误表明数组超出索引。
最佳答案
想通了。我寻找 newValue 的工作涉及使用一些闭包。我必须在闭包内将 i
重新定义为 index
才能在闭包内使用它。
关于arrays - 如何用新字符串替换数组中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809399/