ios-Swift:使用tup在单个切换情况下有多个间隔

像这样的代码:

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

问题是我可以在第二个元组值中使用多个间隔吗?

对于非元组切换,可以很容易地完成

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

我应该使用哪个分隔符来分隔元组内部的间隔,或者它不适用于元组开关,而我必须使用内部开关? 谢谢!

iiFreeman asked 2019-10-08T19:40:34Z
1个解决方案
131 votes

您必须在顶层列出多个元组:

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)")
}
drewag answered 2019-10-08T19:40:45Z
translate from https://stackoverflow.com:/questions/25165123/swift-multiple-intervals-in-single-switch-case-using-tuple