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

标签 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.

}

错误表明数组超出索引。

最佳答案

想通了。我寻找 newValue 的工作涉及使用一些闭包。我必须在闭包内将 i 重新定义为 index 才能在闭包内使用它。

关于arrays - 如何用新字符串替换数组中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809399/

相关文章:

ios - UICollectionViewController - 展开可选值时意外发现 nil

C++ 排序真假数组

java - Java,数组,线程数,最小值,最大值

javascript - 如何更改具有特定 url 的所有 href?

javascript - 查找字符串中最长的单词不起作用

ios - 如何最好地优化 NSLayoutConstraint?

C:所有字符串文字都有静态存储期限吗?

c++ - 比较两个 C++ 十六进制字符串

java - 如何更有效地对单个字符串调用replaceAll两次

ios - 动态改变API数据获取结构