• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift switch语句的高级用法 - 二狗你变了

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

}

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]字符串(String类、NSString类)常用操作发布时间:2022-07-13
下一篇:
Swift版本报错发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap