无法理解为什么在开关例的where子句中,常数x与负常数y进行比较。

问题描述 投票:0回答:1

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

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"
swift swift-playground
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.