Swift switch語句的高級用法


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")

}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM