SWIFT 4中的switch语句在第一个匹配case完成后立即执行里面的语句。
以下是SWIFT 4-中提供的SWITCH语句的一般语法
switch expression { case expression1 : statement(s) fallthrough /*可选 */ case expression2, expression3 : statement(s) fallthrough /* 可选 */ default : /* 可选 */ statement(s); }
如果我们不使用Fallthrough语句,则程序将在执行匹配的case语句后从switch语句中出来。我们将通过以下两个示例来明确其函数。
以下是SWIFT 4编程中不使用Fallthrough-的SWITCH语句示例
链接:https://www.learnfk.comhttps://www.learnfk.com/swift/swift-switch-statement.html
来源:LearnFk无涯教程网
var index=10 switch index { case 100 : print( "Value of index is 100") case 10,15 : print( "Value of index is either 10 or 15") case 5 : print( "Value of index is 5") default : print( "default case") }
编译并执行上述代码时,将生成以下结果-
Value of index is either 10 or 15
以下是使用Fallthrough-进行SWIFT 4编程时的SWITCH语句示例
var index=10 switch index { case 100 : print( "Value of index is 100") fallthrough case 10,15 : print( "Value of index is either 10 or 15") fallthrough case 5 : print( "Value of index is 5") default : print( "default case") }
编译并执行上述代码时,将生成以下结果-
Value of index is either 10 or 15 Value of index is 5
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)