值绑定与 where 子句
绑定匹配到的值,并添加额外条件。
值绑定与 where 子句 是 CoddyKit 上的免费 Swift Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Swift Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Swift Academy 课程共包含 4 节课。
在分支中绑定值
分支语句可以使用 case let x 将匹配到的值绑定到一个常量,使该值能够在分支内部使用。
let value = 42
switch value {
case let n:
print("The number is \(n)")
}绑定元组的一部分
您可以只绑定元组中需要的部分,在特定位置使用 let。
let point = (3, 7)
switch point {
case (let x, 0):
print("On x-axis at \(x)")
case (0, let y):
print("On y-axis at \(y)")
case let (x, y):
print("At \(x), \(y)")
}where 子句
您可以向分支添加 where 子句,对绑定的值应用额外的布尔条件。只有模式和条件都满足时,分支才会匹配。
let number = 8
switch number {
case let n where n % 2 == 0:
print("\(n) is even")
case let n:
print("\(n) is odd")
}范围中的 where
您可以将范围模式与 where 子句组合,以实现更精细的控制。
let score = 95
switch score {
case 90...100 where score == 100:
print("Perfect")
case 90...100:
print("Excellent")
default:
print("Keep trying")
}元组中的 where
where 子句可以比较已绑定的元组元素。
let point = (4, 4)
switch point {
case let (x, y) where x == y:
print("On the diagonal")
case let (x, y):
print("Off diagonal at \(x), \(y)")
}多个 where 条件
where 后面的条件可以是任意布尔表达式,包括 && 和 ||。
let n = 15
switch n {
case let x where x > 10 && x < 20:
print("In the teens-ish range")
default:
print("Outside range")
}带 where 时的顺序
带有 where 子句的分支只有在条件为真时才会匹配;否则,Swift 会继续检查下一个分支。
let temp = 5
switch temp {
case let t where t < 0:
print("Freezing")
case let t where t < 10:
print("Cold")
default:
print("Mild or warm")
}在范围分支中绑定值
您可以绑定一个值,同时使用 where 将它限制在某个范围内。
let age = 67
switch age {
case let a where a >= 65:
print("Senior, age \(a)")
case let a where a >= 18:
print("Adult, age \(a)")
default:
print("Minor")
}使用绑定进行分类
将绑定与 where 结合,可以让一个分支语句同时对值进行分类并输出该值。
let balance = -50
switch balance {
case let b where b < 0:
print("Overdrawn by \(-b)")
case 0:
print("Empty")
case let b:
print("Balance: \(b)")
}绑定的好处
如果不使用绑定,您就需要反复引用原始变量。绑定可以提供一个简洁的局部名称,并且很适合与 where 过滤条件搭配使用。
let input = 21
switch input {
case let n where n.isMultiple(of: 3):
print("\(n) is divisible by 3")
default:
print("Not divisible by 3")
}捕获所有情况的绑定
没有条件的最后一个 case let x 与 default 类似,同时还能让您获得该值。
let code = 17
switch code {
case 0:
print("zero")
case let other:
print("non-zero: \(other)")
}快速检查
测试值绑定和 where。
回顾:值绑定和 where 子句
使用 case let x 绑定匹配到的值,并添加 where 应用额外条件。两者结合后,一个分支语句就可以对值进行分类,并在同一分支中使用这些值。
let n = 6
switch n {
case let x where x > 0: print("positive \(x)")
default: print("non-positive")
}常见问题解答
「值绑定与 where 子句」课时是免费的吗?
是的 — 「值绑定与 where 子句」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Swift Academy 课程的其余内容,请升级到 CoddyKit PRO。 Swift Academy 课程共包含 4 节课。
「值绑定与 where 子句」这节课中我会学到什么?
绑定匹配到的值,并添加额外条件。 你通过在浏览器中直接运行的动手代码来练习 Swift Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Swift Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Swift Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「值绑定与 where 子句」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Swift Academy 课中编写并运行代码吗?
能。每节 Swift Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 对值进行穷尽式 Switch
- 匹配范围与元组
- 值绑定与 where 子句
- 复合分支与贯穿执行