|
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)
- erlang判断语法结构:if/case/guard
erlang 有好几种常用的判断结构语句,如 if.case.guard 等.文章将分别对 if / case /guard 的特点做介绍,以及用例说明 1.if 结构 if Condition 1 ...
- CASE WHEN 及 SELECT CASE WHEN的用法(转)
Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- CASE WHEN 及 SELECT CASE WHEN的用法
CASE WHEN 及 SELECT CASE WHEN的用法 Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WHEN '1' THEN '男' WH ...
- ORACLE CASE WHEN 及 SELECT CASE WHEN的用法
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- CASE WHEN 及 SELECT CASE WHEN的用法(写了一坨烂代码发现两条sql就行了, 哎)
转自:http://blog.sina.com.cn/s/blog_4c538f6c01012mzt.html Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE ...
- 【转载】CASE WHEN 及 SELECT CASE WHEN的用法
原文链接:https://www.cnblogs.com/aipan/p/7770611.html Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WH ...
- 前端学习 第四弹: HTML(一)
前端学习 第四弹: HTML(一) 元素分类:块元素 内联元素 块级元素在浏览器显示时,通常会以新行来开始(和结束). 例子:<h1>, <p>, <ul>, &l ...
- 『PyTorch』第四弹_通过LeNet初识pytorch神经网络_下
『PyTorch』第四弹_通过LeNet初识pytorch神经网络_上 # Author : Hellcat # Time : 2018/2/11 import torch as t import t ...
- jQuery 关于IE9上传文件无法进入后台问题的原因及解决办法(ajaxfileupload.js第四弹)
第四弹的诞生完全不在自己最初的计划之中,是有个网友看了先前关于<ajaxfileupload.js系列>的文章后提出的问题,由于自己一直是用chrome浏览器去测试demo,完全忽略IE浏 ...
随机推荐
- Redis学习笔记3-Redis5个可运行程序命令的使用
在redis安装文章中,说到安装好redis后,在/usr/local/bin下有5个关于redis的可运行程序.以下关于这5个可运行程序命令的具体说明. redis-server Redisserv ...
- TQ2440 学习笔记—— 1、Windows平台下开发工具安装与环境建立
板子:广州天嵌公司的TQ2440,处理器为三星的S3C2440 1.开发工具的安装与环境建立 系统:win7 64位 SecureCRT软件:该软件能够取代Windows中的超级终端,是个非常好的串 ...
- 安装ubuntu后启动黑屏
我是在windows7上的一个空暇盘上安装ubuntu 14.安装后重新启动没有ubuntu的启动项,然后用easybcd生成启动项,重新启动发现果然有,可是选择之后黑屏. 百度半天无果.后来无意发现 ...
- sublime text3编辑器经常使用快捷方式
1.简单语法的自己主动补全 preferences->setting user 输入例如以下代码,保存. { "auto_complete":true, "auto ...
- RPC和微服务
1 什么是RPC 是remote procedure call的缩写. 2 什么是微服务 所谓的微服务就是说,把一个应用分解成一组小的服务,每个服务运行在自己的进程中.每个服务都可以单独部署,可以用自 ...
- Dubbo近况
刚下班看到开发者头条上一篇讲dubbo前世今生的文章,总结的不错,摘录一下. 从2012年10月23日Dubbo 2.5.3发布后,在Dubbo开源将满一周年之际,阿里基本停止了对Dubbo的主要升级 ...
- bzoj4443
二分+二分图匹配 晚上脑子不太好使... 行列模型,填充数量性质,种种迹象告诉我们这是二分图,但是我觉得好像不太科学就弃了网络流... 二分第k大值,转化为求第n-k+1小值,二分求匹配判定即可. # ...
- JavaScript代码优化新工具UglifyJS
jQuery 1.5 发布的时候 john resig 大神说所用的代码优化程序从Google Closure切换到UglifyJS,新工具的压缩效果非常令人满意. UglifyJS 是一个服务端no ...
- C#面向过程之编译原理、变量、运算符
.net基础:.net与C# .net是一个平台 c#是一门语言 .net的用途a.桌面应用程序 b.网站应用程序 c.专业游戏开发(XBOX360) d.嵌入式设备软件开发 e.智能手机APP开发 ...
- maven中的三种工程,以及在idea中构建父子工程。
1.pom工程:用在父级工程或聚合工程中.用来做jar包的版本控制.主要是定义POM文件,将后续各个子模块公用的jar包等统一提出来,类似一个抽象父类 2.war工程:将会打包成war,发布在服务器上 ...
| | | | | |