如何用新字符串替换数组中的字符串?

时间:2015-09-27 15:13:39

标签: arrays string swift swift2

我正在使用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.

}

错误表明数组已超出索引范围。

1 个答案:

答案 0 :(得分:0)

想出来。我找到newValue的工作涉及使用一些闭包。我不得不在封闭内部将i重新定义为index,以便能够在封闭内使用它。