//: Playground - noun: a place where people can play import UIKit // 对区间进行判断
var score = 90
switch score {
case 0:
print("You got an egg!")
case 1..<60:
print("Sorry, you failed.")
case 60..<70:
print("Just passed.")
case 70..<80:
print("Not bad.")
case 80..<90:
print("Good job!")
case 90..<100:
print("Great!")
case 100:
print("Prefect!")
default:
print("something wrong with your score")
} // 对元组进行判断(1)
var coordinate = (1, 1)
switch coordinate {
case (0, 0):
print("It's at origin!")
case (1, 0):
print("It's an unit vector on the positive x-axis.")
case (-1, 0):
print("It's an unit vector on the negative x-axis.")
case (0, 1):
print("It's an unit vector on the positive y-axis.")
case (0, -1):
print("It's an unit vector on the negative y-axis.")
default:
print("It's just an ordinary coordinate")
} // 对元组进行判断(2)
// 可以通过元组的"_"来忽略对元组中某个值的判断
var coordinate2 = (0, 1)
switch coordinate2 {
case (0, 0):
print("It's at origin!")
case (_, 0):
print("(\(coordinate2.0), 0) is on the x-axis.")
case (0, _):
print("(0, \(coordinate2.1)) is on the y-axis.")
case (-2...2, 0...2):
print("the coordinate is (\(coordinate2.0), \(coordinate2.1))")
default:
print("(\(coordinate2.0), \(coordinate2.1)) is just an ordinary coordinate")
} // 对元组进行判断(3)
// value binding
var coordinate3 = (0, 1)
switch coordinate3 {
case (0, 0):
print("It's at origin!")
case (let x, 0):
print("(\(coordinate3.0), 0) is on the x-axis.")
print("The x value is \(x)")
case (0, let y):
print("(0, \(coordinate3.1)) is on the y-axis.")
print("The y value is \(y)")
case (let x, let y):
print("the coordinate is (\(x), \(y))")
} // 对元组进行判断(4)
// where
// 实现在选择的同时进行逻辑操作
var coordinate4 = (3, 3)
switch coordinate4 {
case let (x, y) where x == y:
print("(\(x), \(y)), x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)), x == -y")
case let (x, y):
print("(\(x), \(y))")
} // 对元组进行判断(5)
var courseInfo = ("3-2", "区间运算符")
switch courseInfo {
case (_, let courseName) where courseName.hasSuffix("运算符"):
print("课程<\(courseName)>是介绍运算符的课程")
default :
print("<\(courseInfo.1)>是其他课程")
} // where(6)
var courseName2 = "如何与傻逼相处"
switch courseName2 {
case let str where str.hasSuffix("运算符"):
print("课程<\(courseName2)>是介绍运算符的课程")
default :
print("<\(courseName2)>是其他课程")
} // 对元组进行判断(7)
var coordinate7 = (1, 0)
switch coordinate7 {
case (0, 0):
print("It's at origin!")
fallthrough // fallthrough会在当前case执行完之后继续下一个case
case (_, 0):
print("(\(coordinate7.0), 0) is on the x-axis.")
fallthrough
case (0, _):
print("(0, \(coordinate7.1)) is on the y-axis.")
fallthrough
case (-2...2, 0...2):
print("the coordinate is (\(coordinate7.0), \(coordinate7.1))")
default:
print("(\(coordinate7.0), \(coordinate7.1)) is just an ordinary coordinate")
}