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 ...
随机推荐
- wikioi3363支线剧情(挖个坑,不会做。。。)
3363 支线剧情 题目描述 Description 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往都有很多 ...
- wpa_supplicant使用笔记-wpa_cli iwconfig
还在搞8634,看不到头了..唉.wireless_tools的缺点是不支持WPA认证,所以有必要把wpa_supplicant也移植过来.无线 网卡是基于zydas芯片的,正好可以在网上搜到wpa_ ...
- 使用ChineseLunisolarCalendar 对象由年份获得生肖名,Datetime.now.tostring获得星期几
一:使用ChineseLunisolarCalendar 对象由年份获得生肖名,截图 二:代码 using System; using System.Collections.Generic; usin ...
- haffman树c实现
#include<stdio.h>#include<stdlib.h>#include<string.h>#define N 100#define M 2*N-1t ...
- u盘启动openwrt
opkg update opkg install kmod-usb-ohci kmod-usb2 kmod-fs-ext3 opkg install kmod-usb-storage reboot m ...
- java中字节流和字符流的区别
流分类: 1.Java的字节流 InputStream是所有字节输入流的祖先,而OutputStream是所有字节输出流的祖先.2.Java的字符流 Reader是所有读取字符串输入流的祖先,而 ...
- serialVersionUID的作用以及设置方法(转)
声明:本篇文章是转载的 http://blog.csdn.net/kakaxi_77/article/details/8129070 http://snowlotus.iteye.com/blog/2 ...
- 【微信公众号】将微信公众号消息里的FromUserName即OpenID转成UnionID
最近在调试微信公众号开发者模式,处理公众号消息,收到如下回调消息内容 <xml><ToUserName><![CDATA[gh_29********21]]>< ...
- CDH5 安装过程
一.环境 1.1 操作系统 $ cat /etc/redhat-release CentOS release 6.5 (Final) 1.2 Java环境 $ java -version java v ...
- 【剑指offer】二叉树中和为某一值的路径
转载请注明出处:http://blog.csdn.net/ns_code/article/details/26141815 题目描写叙述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数 ...