swift是否通过语句?例如,如果我做以下
var testVar = "hello" var result = 0 switch(testVal) { case "one": result = 1 case "two": result = 1 default: result = 3 }
是否可以对情况“一”和情况“二”执行相同的代码?
是。您可以这样做:
var testVar = "hello" var result = 0 switch(testVal) { case "one","two": result = 1 default: result = 3 }
或者,您可以使用fallthrough关键字:
var testVar = "hello" var result = 0 switch(testVal) { case "one": fallthrough case "two": result = 1 default: result = 3 }