ios Swift:使用元组在单个开关案例中的多个间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25165123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:44:34  来源:igfitidea点击:

Swift: Multiple intervals in single switch-case using tuple

iosswiftswitch-statementtuplesxcode6

提问by iiFreeman

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!

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

回答by drewag

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