//: 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")
}

  

Swift - 24 - switch语句的高级用法的更多相关文章

  1. Linux之shell脚本for、while、case语句的高级用法

    1.case语句的用法: [root@ELK-chaofeng test]# cat test3.sh #!/bin/bash while true ;do read -p "please ...

  2. switch语句中default用法详解

    当年学C语言switch开关语句的时候,很多人会告诉你它是这么用的: switch(表达式){ case常量表达式1: 语句1;break; case常量表达式2: 语句2;break; - case ...

  3. Swift中switch强大的模式匹配

    不少人觉得Swift中switch语句和C或C++,乃至ObjC中的差不多,此言大谬! 让本猫带领大家看一下Swift中switch语句模式匹配的威力. 所谓模式匹配就是利用一定模式(比如couple ...

  4. switch语句的妙用

    switch语句的普通用法很简单,如下: var a = 3; switch (a) { case 1: console.log(a); break; case 2: case 3: console. ...

  5. nmap高级用法

    nmap在信息收集中起着很大的作用,今天我来总结一些nmap常用的一些命令 常用探测主机存活方式 1.-sP:进行ping扫描 打印出对ping扫描做出响应的主机,不做进一步测试(如端口扫描或者操作系 ...

  6. Go-函数高级使用-条件分支-包管理-for循环-switch语句-数组及切片-与或非逻辑符

    目录 科普 python 注释 # 函数高级 if else if else 包管理 下载第三方包 比较热门的框架 for 循环 for 循环的几种写法 switch 语句 数组及数组切片 数组迭代 ...

  7. PHP switch的“高级”用法详解

    只所以称为“高级”用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实还是它的基础用法! switch 语句和具有同样表达式的一系列的 IF 语句相似.很多场合下需要把同一个变 ...

  8. Java-Annotation的一种用法(消除代码中冗余的if/else或switch语句)

    Java-Annotation的一种用法(消除代码中冗余的if/else或switch语句) 1.冗余的if/else或switch ​ 有没有朋友写过以下的代码结构,大量的if/esle判断,来选择 ...

  9. 提高java编程质量 - (五)switch语句break不能忘以及default不同位置的用法

    先看一段代码: public class Test{ public static void main(String[] args){ System.)); } } public static Stri ...

随机推荐

  1. [Mon Feb 10 15:21:06 2014] [notice] child pid 7101 exit signal File size limit exceeded (25)

    今天遇到的问题: LAMP的LOG里报如下错误. 然后IE和FIREFOX里显示连接被重置或是无法访问. 但自己建一个正常的PHP测试探针倒可以. 原来是PHP错误日志太多,无法写入LOG导致. [r ...

  2. VSTO:无法安装此应用程序,因为已安装具有相同标识的应用程序

    原文:VSTO:无法安装此应用程序,因为已安装具有相同标识的应用程序 在开发环境(Win7+VS2010+Outlook 2010)上做一个Outlook AddIn,运行Outlook时报如下错误: ...

  3. SpringMvc配置 导致实事务失效

    SpringMVC回归MVC本质,简简单单的Restful式函数,没有任何基类之后,应该是传统Request-Response框架中最好用的了. Tips 1.事务失效的惨案 Spring MVC最打 ...

  4. 13. vs2010 ClientID bug处理

    在VS2010中的产生ClientID有几种方式,每个控件或页面有个ClientIDMode属性,可以用来决定产生ClientID的方式,它有AutoID,Static,Inherit,Predict ...

  5. 【Linux】鸟哥的Linux私房菜基础学习篇整理(七)

    1. test命令的测试功能.测试的标志:(1)关于文件类型的检测 test [-efdbcSpL] filename-e:该文件名是否存在:-f:该文件名是否为文件:-d:该文件名是否为目录:-b: ...

  6. How to add alias on Mac(It's common for most system)

    Since these files are hidden you will have to do an ls -a to list them. If you don't have one you ca ...

  7. OpenWrt简要刷机教程

    准备工作 1. 下载openwrt中文固件到PC.(当然其他英文固件也可) 2  找到路由器的RST键. 3  找到路由器刷机口---姑且称之为“WAN口” 4. 关闭路由器的电源. 5. 将PC网口 ...

  8. libvirt基于安装

  9. [转载]10 Best Tools For Websites And Apps Development Ever

    转载自: http://www.websurfmedia.com/10-best-tools-for-websites-and-apps-development-ever/   The world i ...

  10. CCASS四种交收指令

    CCASS 提供了4种类型的指令:ATI: Account Transfer Instruction 账户转移指令,用于本券商各个仓位上的转移STI: Stock Segregate Account ...