使用开关/案例进行数组索引

时间:2016-04-10 12:02:21

标签: arrays swift count switch-statement

我正在尝试获取数组中的索引计数并构建计数的情况,但我无法正确构建它,也无法在线或在文档中找到任何内容。到目前为止,我有以下内容......

 for array.count in theArray {
     switch array.count {
     case 1...5 :
       //do something
     case 6...10 :
       //do something
     case > 10 :
       //do something
     default :
       //do something
     } //close for switch        
 }//close for for/in

1 个答案:

答案 0 :(得分:3)

而不是array.count只使用数组,因为它是数组元素theArray

let theArray = [[1,2,3,4,5,6,8,7,3,5,6],[1,2,3,4,5,6,8,7,3,5,6],[1,2,3,4,5,6,8,7,3,5,6]]

for array in theArray {
    switch array.count {
    case 1...5 : print("Under 5")
    case 6...10 :  print("Between 6 to 10")
    case let count where count > 10 /*or 10..<Int.max*/ : print("More than 10")
    default : break
    }
}