Swift - 24 - switch语句的高级用法
//: 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语句的高级用法的更多相关文章
- Linux之shell脚本for、while、case语句的高级用法
1.case语句的用法: [root@ELK-chaofeng test]# cat test3.sh #!/bin/bash while true ;do read -p "please ...
- switch语句中default用法详解
当年学C语言switch开关语句的时候,很多人会告诉你它是这么用的: switch(表达式){ case常量表达式1: 语句1;break; case常量表达式2: 语句2;break; - case ...
- Swift中switch强大的模式匹配
不少人觉得Swift中switch语句和C或C++,乃至ObjC中的差不多,此言大谬! 让本猫带领大家看一下Swift中switch语句模式匹配的威力. 所谓模式匹配就是利用一定模式(比如couple ...
- switch语句的妙用
switch语句的普通用法很简单,如下: var a = 3; switch (a) { case 1: console.log(a); break; case 2: case 3: console. ...
- nmap高级用法
nmap在信息收集中起着很大的作用,今天我来总结一些nmap常用的一些命令 常用探测主机存活方式 1.-sP:进行ping扫描 打印出对ping扫描做出响应的主机,不做进一步测试(如端口扫描或者操作系 ...
- Go-函数高级使用-条件分支-包管理-for循环-switch语句-数组及切片-与或非逻辑符
目录 科普 python 注释 # 函数高级 if else if else 包管理 下载第三方包 比较热门的框架 for 循环 for 循环的几种写法 switch 语句 数组及数组切片 数组迭代 ...
- PHP switch的“高级”用法详解
只所以称为“高级”用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实还是它的基础用法! switch 语句和具有同样表达式的一系列的 IF 语句相似.很多场合下需要把同一个变 ...
- Java-Annotation的一种用法(消除代码中冗余的if/else或switch语句)
Java-Annotation的一种用法(消除代码中冗余的if/else或switch语句) 1.冗余的if/else或switch 有没有朋友写过以下的代码结构,大量的if/esle判断,来选择 ...
- 提高java编程质量 - (五)switch语句break不能忘以及default不同位置的用法
先看一段代码: public class Test{ public static void main(String[] args){ System.)); } } public static Stri ...
随机推荐
- Sequence《优先队列》
Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...
- 应用程序的关闭退出(在FMX中,Activity替代了Form的概念)
在VCL中,关闭程序的主窗体也就意味着程序的主循环结束,主程序自然而然结束.所以在主窗体中使用窗体的关闭函数(Close)即可,如下: procedure TfrmMain.btncloseClick ...
- 利用Java反射机制完成XML到对象的解析
对于一些小批量的数据,如果采用数据库来存取的话,未免有点大题小作,使用XML文件是个不错的方法,尤其是在一些Web应用中,经常需要缓存一部分数据,如果将这些数据形成XML文件,解析后放入一个Hasht ...
- spj 设计
在数据库中使用SELECT INTO 语句从SP.J.P表中创建一个新表J_P_SPJ新表J_P_QTY中的属性列包括:工程号 (JNO).工程名(JNAME).零件号(PNO).零件名(PNAME) ...
- baidu面试题
百度:http://blog.chinaunix.net/uid-26602509-id-3306451.html http://lvwenwen.iteye.com/blog/1504379
- 423. Reconstruct Original Digits from English
这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...
- 理解RESTful架构(转)
理解RESTful架构 作者: 阮一峰 http://www.ruanyifeng.com/blog/2011/09/restful 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这 ...
- Mysql操作命令出现错误时消除/mysql数据导入txt
MySQL怎样取消错误命令 http://jingyan.baidu.com/album/546ae1851e9fd61149f28cef.html?picindex=6 mysql导入数据load ...
- Mac内建Apache
打开终端 重启apache:sudo /usr/sbin/apachectl restart 关闭apache:sudo /usr/sbin/apachectl stop 开启apache:sudo ...
- ubuntu firefox 选中变成了删除
在ubuntu下 我的firefox浏览器出现了问题.描述: 1.在firefox中所有能够删除的文字只要选中就自动删除了. 终端中 ibus-setup勾掉在应用窗口中启用内嵌编辑模式