Swift: Multiple intervals in single switch-case using tuple

IosSwiftSwitch StatementTuplesXcode6

Ios Problem Overview


Have a code like:

switch (indexPath.section, indexPath.row) {
    case (0, 1...5): println("in range")
    default: println("not at all")
}

The question is can I use multiple intervals in second tuple value?

for non-tuple switch it can be done pretty easily like

switch indexPath.section {
case 0:
    switch indexPath.row {
    case 1...5, 8...10, 30...33: println("in range")
    default: println("not at all")
    }
default: println("wrong section \(indexPath.section)")
}

Which separator should I use to separate my intervals inside tuple or it's just not gonna work for tuple switches and I have to use switch inside switch? Thanks!

Ios Solutions


Solution 1 - Ios

You have to list multiple tuples at the top level:

switch (indexPath.section, indexPath.row) {
    case (0, 1...5), (0, 8...10), (0, 30...33):
        println("in range")
    case (0, _):
        println("not at all")
    default:
        println("wrong section \(indexPath.section)")
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniiFreemanView Question on Stackoverflow
Solution 1 - IosdrewagView Answer on Stackoverflow