【问题标题】:How to use a tuple as a pattern in a switch statement如何在 switch 语句中使用元组作为模式
【发布时间】:2020-03-26 14:12:40
【问题描述】:

是否可以在 switch 语句中使用元组作为模式?

在下面的示例代码中,我想定义一个命名常量来表示要匹配的模式,在本例中为元组 (1,2)

let thing = 1
let otherThing = 2
let patternToMatchAgainst = (1,2)

switch (thing, otherThing) {
  case patternToMatchAgainst: break // Expression pattern of type '(Int, Int)' cannot match values of type '(Int, Int)'
  default: break
}

【问题讨论】:

标签: swift tuples pattern-matching


【解决方案1】:

你可以这样尝试: 案例(patternToMatchAgainst.0,patternToMatchAgainst.1):

【讨论】:

    猜你喜欢
    • 2014-01-06
    • 1970-01-01
    • 2010-09-26
    • 2017-11-05
    • 2021-12-10
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多