2016-06-06 7388

作者:Olivier Halligon,原文链接,原文日期:2016-05-16

译者:walkingway;校对:Cee;定稿:numbbbbb

现在我们来重新回顾下前三弹模式匹配的各种语法 第一弹第二弹第三弹,第四弹是本系列的最后一篇文章,本章会教大家使用 if case let,for case where 等一些高级语法,让我们拭目以待吧!

本篇文章会结合本系列前三篇文章提到的语法,然后将它们应用在一些更先进表达式中。

这篇文章是模式匹配系列文章的最后一部分,你可以在这里阅读所有内容:第一弹第二弹第三弹第四弹

if case let

语句 case let x = y 模式允许你检查 y 是否能匹配 x。

而 if case let x = y { … } 严格等同于 switch y { case let x: … }:当你只想与一条 case 匹配时,这种更紧凑的语法尤其有用。有多个 case 时更适合使用 switch。

例如,我们有一个与之前文章类似的枚举数组:

casecasecase然后我们可以这样写:

m = Media.Movie(title: , director: , year: )

Media.Movie(title, , ) = m {

print"This is a movie named \(title)"改用 switch 后更冗长的版本:

m {

caselet__print"This is a movie named \(title)"default// do nothing, but this is mandatory as all switch in Swift must be exhaustive

if case let where

我们当然还可以将 if case let 和 where 从句组合在一起用:

Media.Movie(, , year) = m year < {

print"Something seems wrong: the movie's year is before the first movie ever made."这种方式可以组合成一个相当强大的表达式,而改用 switch 实现可能会变得非常复杂,需要写很多行代码来检测那一个特定的 case。

guard case let

当然,guard case let 类似于 if case let,你可以使用 guard case let 和 guard case let … where … 来确保匹配一个模式或一个条件,而当无法匹配模式或满足条件时就退出。

casecase

(response: NetworkResponse)guardcaseletletaswhere200300elseprint"Invalid response, can't process"return

}

print"Processing \(data.length) bytes…"/* … */

for case

将 for 和 case 组合在一起也能让你有条件地遍历一个集合对象。使用 for case … 语义上类似于 for 循环,而且将它整个循环体封装在了 if case 的结构之中:它只会遍历、处理那些模式匹配了的元素。

mediaList: [Media] = [

"Harry Potter and the Philosopher's Stone""J.K. Rowling"1997"Harry Potter and the Philosopher's Stone""Chris Columbus"2001"Harry Potter and the Chamber of Secrets""J.K. Rowling"1999"Harry Potter and the Chamber of Secrets""Chris Columbus"2002"Harry Potter and the Prisoner of Azkaban""J.K. Rowling"1999"Harry Potter and the Prisoner of Azkaban""Alfonso Cuarón"2004"J.K. Rowling: A Year in the Life""James Runcie"2007"https://en.wikipedia.org/wiki/List_of_Harry_Potter-related_topics"

()

Media.Movie(title, , year) mediaList {

print" - \(title) (\(year))"

- Harry Potter and the Philosopher's Stone (2001)

- Harry Potter and the Chamber of Secrets (2002)

- Harry Potter and the Prisoner of Azkaban (2004)

- J.K. Rowling: A Year in the Life (2007)

for case where

为 for case 增加一个 where 从句,能使其变得更加强大:

()

Media.Movie(title, director, year) mediaList director == {

print" - \(title) (\(year))"

- Harry Potter and the Philosopher's Stone (2001)

- Harry Potter and the Chamber of Secrets (2002)

模式匹配第四弹:if case,guard case,for case的更多相关文章

  1. erlang判断语法结构:if/case/guard

    erlang 有好几种常用的判断结构语句,如 if.case.guard 等.文章将分别对 if / case /guard 的特点做介绍,以及用例说明 1.if 结构 if Condition 1  ...

  2. CASE WHEN 及 SELECT CASE WHEN的用法(转)

    Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END   ...

  3. CASE WHEN 及 SELECT CASE WHEN的用法

    CASE WHEN 及 SELECT CASE WHEN的用法 Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WHEN '1' THEN '男' WH ...

  4. ORACLE CASE WHEN 及 SELECT CASE WHEN的用法

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  5. CASE WHEN 及 SELECT CASE WHEN的用法(写了一坨烂代码发现两条sql就行了, 哎)

    转自:http://blog.sina.com.cn/s/blog_4c538f6c01012mzt.html Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE  ...

  6. 【转载】CASE WHEN 及 SELECT CASE WHEN的用法

    原文链接:https://www.cnblogs.com/aipan/p/7770611.html Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WH ...

  7. 前端学习 第四弹: HTML(一)

    前端学习 第四弹: HTML(一) 元素分类:块元素 内联元素 块级元素在浏览器显示时,通常会以新行来开始(和结束). 例子:<h1>, <p>, <ul>, &l ...

  8. 『PyTorch』第四弹_通过LeNet初识pytorch神经网络_下

    『PyTorch』第四弹_通过LeNet初识pytorch神经网络_上 # Author : Hellcat # Time : 2018/2/11 import torch as t import t ...

  9. jQuery 关于IE9上传文件无法进入后台问题的原因及解决办法(ajaxfileupload.js第四弹)

    第四弹的诞生完全不在自己最初的计划之中,是有个网友看了先前关于<ajaxfileupload.js系列>的文章后提出的问题,由于自己一直是用chrome浏览器去测试demo,完全忽略IE浏 ...

随机推荐

  1. Django打造大型企业官网(一)

    一.nvm的安装 (1)下载:nvm1.16 (2)安装完成后添加环境变量 C:\Users\Administrator\AppData\Roaming\nvm (3)修改settings.txt,将 ...

  2. ssh 远程登陆指定port

    ssh 到指定port  ssh -p xx user@ip      xx 为 port号    user为username   ip为要登陆的ip

  3. 状压DP问题

    状态压缩·一 题目传送:#1044 : 状态压缩·一 AC代码: #include <map> #include <set> #include <list> #in ...

  4. Unix/Linux 软件安装

    Unix/Linux 软件安装 首先我们明确.在Unix like的机器上,一套软件并不唯独一个程序,而是一堆程序代码文件. 比如main.c,haha.c,sin_value.c这三个源码文件. 1 ...

  5. Microsoft Windows CE 5.0 Board Support Package, Boot Loader, and Kernel Startup Sequence

    Summary Learn about the initial, low-level startup sequence and the hardware platform functions that ...

  6. SGU 261. Discrete Roots (N次剩余)

    N次剩余 题目:http://acm.sgu.ru/problem.php? contest=0&problem=261 题意:给定n,a,p 求出x^n ≡ a(mod p)在模p意义下的全 ...

  7. Email-ext plugin

    https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin General This plugin extends Jenkins built i ...

  8. 转 source insight 复制后光标在前面

    source insight 里编辑的时候,每次粘贴后,光标停留在粘贴内容的前面. 我想把它设定为 粘贴后,光标移动倒粘贴内容的后面. 怎么做? 这是个设置问题,按照下面的步骤设定就可以了. Opti ...

  9. BZOJ_1576_[Usaco2009 Jan]安全路经Travel&&BZOJ_3694_最短路_树链剖分+线段树

    Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...

  10. bzoj 2763: [JLOI2011]飞行路线【分层图+spfa】

    为什么早年的题总是从0开始标号啊--又zz了一次WA 分层图的题只有这一个套路吧,建分层图,然后优化时间是分层跑spfa然后层与层之间单独跑即可 #include<iostream> #i ...