swift - 使用 for 循环时的预期声明

标签 swift loops

<分区>

我只是想使用 for 循环来运行一些代码,但我收到了预期的声明错误。

   var index = 0

    for( i in 0..< array.count) {

        let commonPrefix = array[i].commonPrefixWithString(array[index], options: .CaseInsensitiveSearch)

        if (countElements(commonPrefix) == 0 ) {

            let string = array[index].uppercased();

            let firstCharacter = string[string.startIndex]

            let title = "\(firstCharacter)"

            let newSection = (index: index, length: i - index, title: title)

            sections.append(newSection)

            index = i

        }

    }

帮助

最佳答案

这不是您在 Swift 中编写 for 循环的方式。你要的是这个

for i in 0..<array.count {
    // the loop inner body
}

关于swift - 使用 for 循环时的预期声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43244369/

相关文章:

swift - 新的 Key-Value Observing (KVO) 只调用 .initial 而不是 .new

ios - CollectionView应该用动画调整滚动位置

bash - 在 bash 的 5,000 个目录中创建 5,000,000,000 个空文件的最快方法

javascript - 停止循环函数setTimeout

java - 循环条件,最佳实践?

python - python中的c风格循环

ios - 使用 Interface Builder/Storyboards 中的自动布局创建网格类型布局

ios - 无法在 iPhone 版 Whatsapp 上分享网址

ios - 为什么将对象追加到数组时会出现 nil 错误?

php - 带有键的数组的 Twig for 循环