首页 > 解决方案 > 无法理解为什么将常数 x 与 switch 案例中 where 子句中的负常数 y 进行比较

问题描述

我正在学习并浏览 Swift 文档中的控制流部分。我遇到了一大段我不明白的代码。我的问题是为什么在第 5 行, y 的常量值在 where 子句中设置为负数。

let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
  print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
  print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
  print("(\(x), \(y)) is just some arbitrary point")
}
// Prints "(1, -1) is on the line x == -y"

标签: swiftswift-playground

解决方案


推荐阅读