//: 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. GPIO软件模拟I2C

    /***************************************************************************** * * Filename: * ----- ...

  2. 几个国外FMX网站 good

    FireMonkey X –  Amazing overview of FireMonkey FMX Feeds – All your FireMonkey news in one place FMX ...

  3. 【HDOJ】2699 Five in a Row

    wa了几次,至少要考虑4个方向:下.右.左下.右下.很像当年北航的机试题目. /* 2699 */ #include <iostream> #include <cstdio> ...

  4. COJ 3007 Mr.Yang的小助手

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1037 试题描述: 信息学社团已经逐渐发展壮大,成员也越来越多.现在,有n个 ...

  5. 【Gzip】

    为你的网站开启 gzip 压缩功能(nodejs.nginx) Do not forget to use Gzip for Express.js 网页GZIP压缩检测

  6. 快速傅里叶变换(FFT):COGS 2216. 你猜是不是KMP

    2216. 你猜是不是KMP ★★★☆   输入文件:guess.in   输出文件:guess.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] XX在玩两个串的游戏. ...

  7. 【宽搜】Vijos P1360 八数码问题

    题目链接: https://vijos.org/p/1360 题目大意: 3x3格子上放1~8数字,一个空位,每次空位可与上下左右交换,固定终止布局,求输入的起始布局需要几步到达终止布局 题目思路: ...

  8. CentOS-6.4-i386硬盘安装

    由于安装程序不能识别NTFS分区上的镜像,因此需要变通,网上可参考的文章分为两种方法: 1.使用分区工具创建EXT分区,再用Windows下可访问EXT分区的软件,将安装镜像拷入进行安装. 2.使用分 ...

  9. zoj 2404 最小费用流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2404 #include <cstdio> #incl ...

  10. selenium python presence_of_element_located vs visibility_of_element_located

    背景: 用WebDriverWait时,一开始用的是presence_of_element_located,我对它的想法就是他就是用来等待元素出现.结果屡屡出问题.元素默认是隐藏的,导致等待过早的就结 ...