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

  

时间: 2024-08-07 23:05:40

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

&lt;06&gt;变量使用前易犯错误总结+if语句介绍及基本格式+if-else语句及嵌套+if语句的高级用法+if语句的使用注意点+arc4random_uniform 函数导入一个头文件&lt;stdlib.h&gt;+

1)变量要初始化 -1 0  ,或者1 依据程序而定 ---------------------------------- if语句介绍及基本格式 分支语句结构 1)if 格式: if(表达式/常量/变量){ 语句块1; } 原理: 1)先计算小括号中的表达式的值 真(1) 会执行 大括号中的语句 假(0) 不会执行 大括号中的语句 2) switch 1 #include <stdio.h> 2 3 int main(int argc, const char * argv[]) { 4 5

Swift中switch强大的模式匹配

不少人觉得Swift中switch语句和C或C++,乃至ObjC中的差不多,此言大谬! 让本猫带领大家看一下Swift中switch语句模式匹配的威力. 所谓模式匹配就是利用一定模式(比如couple)去定制自己需要的匹配结果,比如以下一个3D空间的点坐标: let point3D:(x:Int,y:Int,z:Int) = (1,2,3) 我们首先做一些简单的匹配,只是简单的比较是否等于或不等于指定的常量: switch(point3D){ case (0,0,0):print("Origin

OC与Swift在判读语句中的区别

11.swift中的switch结构 区别一: oc中switch条件只可以放整数 swift中switch条件可以放几乎任何数据类型 区别二: oc中每一个case中应有break,如果没有break,则会继续向下贯穿执行case直到碰见break跳出switch结构 swift中不需要在case分支最后添加break语句,该case分支执行完成后,会跳出switch结构 区别三: oc中不是每个case后面必须有可执行语句,如果某个case分支中没有可执行语句和break,则该分支将会执行其

switch语句使用中遇到的问题

switch语句的常见用法为: switch(expression) { case n: code block break; case n: code block break; default: code block } 执行过程为根据expression对每个case按顺序进行匹配,如果匹配到,则执行case中代码,如果未出现break则继续进行匹配. switch的另一种用法为: switch (expression) { case n: case n: code block; break;

Swift语言中的switch语句的妙用

Swift中的switch语句的类另用法: // 强大的switch语句 var a:Int = 100 switch a { case a where a < 0: println("Negative") case a where a == 0: println("Zero") case a where a > 0: println("Positive") default: println("Unknow") }

PHP switch的“高级”用法详解

只所以称为“高级”用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实还是它的基础用法! switch 语句和具有同样表达式的一系列的 IF 语句相似.很多场合下需要把同一个变量(或表达式)与很多不同的值比较,并根据它等于哪个值来执行不同的代码.这正是 switch 语句的用途. 注意: 注意和其它语言不同,continue 语句作用到 switch 上的作用类似于 break.如果在循环中有一个 switch 并希望 continue 到外层循环中的下一个轮回,用 co

Swift的switch结构中,fallthrough的用法注意总结

在swift的switch中,case后面加了fallthrough的用法,就和OC的case后面没加break的用法是一样的! 使用fallthrough需要注意的有: 1.加了fallthrough后,会直接运行[紧跟的后一个]case或default语句,不论条件是否满足都会执行 var age = 10 switch age { case 0...10: print("小朋友") fallthrough case 11...20: print("大朋友")

switch语句用法规则

switch也是c语言中比较常见的一种函数,前面学这个的时候只是记了一个大概的用法,也没怎么练习 今天上c语言实训课的时候被同学问了一个关于switch的题目,我选错了.所以回来重新整理一下这个函数的要点. 先来看一下今天他问那个题目: 例题:若有一下变量定义: float x; int a, b; 则正确的switch语句是( ). A.switch(x){ B.switch(a){ C.switch(b){ D.switch(a+b){ case 1.0:printf("*\n")

go语言switch语句用法

switch是最灵活的一种控制语句,在使用switch时候,需要注意: --左花括号"{"必须与switch处于同一行 --条件表达式不限制为常量或者整数 --单个case中,可以出现多个结果选项 --与C语言等规则相反,G0语言不需要break来明确退出一个case --只有在case中明确添加fallthrough关键字,才会继续执行紧跟的下一个case --可以不设定switch之后的条件表达式,在此种情况下,整个switch结构与多个if....else的逻辑作用等同 --可以