Swift:使用tuple在单个开关情况下的多个间隔

时间:2022-09-25 15:26:27

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!

我应该用哪一个分隔符来分隔元组内部的间隔或者它对元组交换机不起作用我必须在开关内部使用开关?谢谢!

1 个解决方案

#1


102  

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)")
}

#1


102  

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)")
}