swift switch选择结构

本文详细介绍了Swift语言中Switch语句的多种用法,包括基本语法、复合值匹配、类型判断、范围判断、元组匹配及Fallthrough的使用等,并通过实例说明了如何灵活运用这些特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、一般用法

swift语言的switch中自带break,所以使用时可以省略不写
swift语言中case必须包含所有情况,否则必须有default关键字

// if - else
let rating = "A"
if rating == "A"{
    print("Great!")
}
else if rating == "B"{
    print("Just so-so")
}
else if rating == "C"{
    print("It's Bad")
}
else{
    print("Error")
}


// switch
switch rating{
case "A":
    print("Great!")
case "B":
    print("Just so-so")
case "C":
    print("It's Bad")
default:
    print("Error")
}

2、在一个case中判断多个值

// 在一个case中判断多个值
switch rating{
case "a","A":
    print("Great!")
case "b","B":
    print("Just so-so")
case "c","C":
    print("It's Bad")
default:
    print("Error")
}

3、使用switch判断其他类型

别的语言switch只能判断整形,swift能判断所有类型

// 使用switch判断浮点数
let x = 2.8
switch x{
case 2.8:
    print("I'm 2.8")
default:
    print("I'm not 2.8")
}


// 使用switch判断布尔值
let y = true
switch y{
case true:
    print("I'm true")
default:
    print("I'm false")
}

4、case后面可以写一个范围

let score = 90
switch score{
case 0:
    print("You got an egg!")
case 1..<60:
    print("You failed.")
case 60:
    print("Just passed")
case 61..<80:
    print("Just so-so")
case 80..<90:
    print("Good")
case 90..<100:
    print("Great!")
case 100:
    print("Perfect!")
default:
    print("Error score.")
}

5、switch与元组的结合使用

let vector = (1,1)
switch vector{
case (0,0):
    print("It's origin!")
case (1,0):
    print("It an unit vector on the positive x-axis.")
case (-1,0):
    print("It an unit vector on the negative x-axis.")
case (0,1):
    print("It an unit vector on the positive y-axis.")
case (0,-1):
    print("It an unit vector on the negative y-axis.")
default:
    print("It's just an ordinary vector.")
}

//与元组解包结合使用

let point = (1,1)
switch point{
case (0,0):
    print("It's origin!")
case (_,0):
    print("It on the x-axis.")
case (0,_):
    print("It on the y-axis.")
default:
    print("It's just an ordinary point.")
}


switch point{
case (0,0):
    print("It's origin!")
case (_,0):
    print("It on the x-axis.")
case (0,_):
    print("It on the y-axis.")
case (-2...2,-2...2):
    print("It's near the origin.")
default:
    print("It's just an ordinary point.")
}


// Value binding
switch point{
case (0,0):
    print("It's origin!")
case (let x,0):
    print("It on the x-axis.")
    print("The x value is \(x)")
case (0,let y):
    print("It on the y-axis.")
    print("The y value is \(y)")
case (let x,let y):
    print("It's just an ordinary point.")
    print("The point is ( \(x) , \(y) )")
}

6、fallthrough的使用

fallthrough可以取消break,是程序语句执行下去
注意:fallthrough 并不会判断下一个case(或者default)是否符合switch的条件,而是直接跳到下一个case(或者default)。

switch point{
case (0,0):
    print("It's origin!")
    fallthrough
case (_,0):
    print("It's on the x-axis.")
    fallthrough
case (0,_):
    print("It's on the y-axis.")
    fallthrough
case (-2...2,-2...2):
    print("It's near the origin.")
    fallthrough
default:
    print("It's just an ordinary point.")
}
/**
 It's origin!
 It's on the x-axis.
 It's on the y-axis.
 It's near the origin.
 It's just an ordinary point.
 */

// 注意:fallthrough 并不会判断下一个case(或者default)是否符合switch的条件,而是直接跳到下一个case(或者default)。

// 所以,首先,fallthrough不能跳转到一个有逻辑的case中
// 下面的程序报错
//let num = 3
//switch num{
//case -3…3:
// print(“small number”)
// fallthrough
//case let x where x%2 == 1:
// print(“odd number”)
//}

// 其次,不要用switch和fallthrough组合复杂的判断逻辑
// fallthrough应该用于从一般到特殊的判定
// 以下代码的输出,就是错误的。

let num = 3
switch num{
case -3...3:
    print("small number")
    fallthrough
case 0:
    print("zero")
    fallthrough
case -100..<0:
    print("negative number")
    fallthrough
case 1...100:
    print("positive number")
    fallthrough
default:
    print("a number")
}
/**
 small number
 zero
 negative number
 positive number
 a number
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值