2017-04-06 46 views
1

我只是试图使用for循环来运行某些代码,但我得到了一个Expected声明错误。使用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 

     } 

    } 

帮助

+0

而在什么行/列? – Evert

+0

定义“sections”在哪里? – Evert

+0

* newSection *是什么类型? – dfd

回答

0

这不是你如何写在斯威夫特一个for循环。你想要的是这样的

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

在Swift中你应该得到数组索引属性'for i in array.indices'。这种方式,如果你迭代一个数组切片它不会崩溃你的应用程序 –