Swift switch语句的高级用法
p.p2 { margin: 0; font: 15px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1); min-height: 18px }
p.p3 { margin: 0; font: 15px Menlo; color: rgba(0, 116, 0, 1); background-color: rgba(255, 255, 255, 1) }
p.p4 { margin: 0; font: 15px Menlo; color: rgba(170, 13, 145, 1); background-color: rgba(255, 255, 255, 1) }
p.p5 { margin: 0; font: 15px Menlo; color: rgba(196, 26, 22, 1); background-color: rgba(255, 255, 255, 1) }
p.p6 { margin: 0; font: 15px Menlo; color: rgba(63, 110, 116, 1); background-color: rgba(255, 255, 255, 1) }
span.s1 { color: rgba(170, 13, 145, 1) }
span.s2 { color: rgba(28, 0, 207, 1) }
span.s3 { color: rgba(0, 0, 0, 1) }
span.s4 { color: rgba(63, 110, 116, 1) }
span.s5 { color: rgba(46, 13, 110, 1) }
span.s6 { color: rgba(196, 26, 22, 1) }
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, 0)
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, 4)
switch coordinate3 {
case (0, 0):
print(“It\’s ar 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, let y) where x == y:
print(“(\(x),\(y)),x==y”)
case (let x, let y) where x == -y:
print(“(\(x), \(y)), x == -y”)
case (let x, let 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 = “如何学习Swift”
switch courseName2 {
case let str where str.hasSuffix(“Swift”):
print(“课程<\(courseName2)>是介绍Swift的课程”)
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”)
}