rowenarrow
rowenarrow

Reputation: 67

Using a switch/case for array index

I am trying to get the count of the indexes in an array and build cases of of the count but I am not able to build it properly nor an I find anything online or in the docs about it. So far I have the following...

 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

Upvotes: 0

Views: 2243

Answers (1)

Rahul Katariya
Rahul Katariya

Reputation: 3628

Instead of array.count use only array as it is a Element of Array 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
    }
}

Upvotes: 4

Related Questions