arrays - 对数组索引使用 switch/case

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

最佳答案

而不是 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/

相关文章:

java - 是否可以将几个 StringArray 值写入一个 String 中?

ios - 如何使用AFNetworking获取URL的内容?

ios - 注册时将用户添加到角色

mysql : select count when id have multiple same value

javascript - 数组名称可以被视为变量吗

arrays - 排序数组和查找更改 - Swift

macos - Swift 时间间隔格式化程序

mysql - 统计不同表数据库的行数

javascript - 统计来自 Discord 服务器的消息 - Discord.js

arrays - 如何使类型化数组符合协议(protocol)