我正在尝试获取数组中索引的计数并构建计数的案例,但我无法正确构建它,也无法在网上或在文档中找到任何关于它的信息。到目前为止,我有以下...
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
最佳答案
而不是 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
}
}
关于arrays - 对数组索引使用 switch/case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529448/