swift语言点评四-Closure
总结:整个Closure的作用在于简化语言表述形式。
一、闭包的简化
Closure expression syntax has the following general form:
- { () -> in
- }
- reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
- return s1 > s2
- })
Because the sorting closure is passed as an argument to a method, Swift can infer the types of its parameters and the type of the value it returns.
reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )
Implicit Returns from Single-Expression Closures
reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )
Shorthand Argument Names
Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.
reversedNames = names.sorted(by: { $0 > $1 } )
Operator Methods
reversedNames = names.sorted(by: >)
二、拖尾变换
- func someFunctionThatTakesAClosure(closure: () -> Void) {
- // function body goes here
- }
- // Here's how you call this function without using a trailing closure:
- someFunctionThatTakesAClosure(closure: {
- // closure's body goes here
- })
- // Here's how you call this function with a trailing closure instead:
- someFunctionThatTakesAClosure() {
- // trailing closure's body goes here
- }
闭包的实现在函数参数列表外;
再次精简
reversedNames = names.sorted() { $0 > $1 }
reversedNames = names.sorted { $0 > $1 }
完全移出
- let strings = numbers.map { (number) -> String in
- var number = number
- var output = ""
- repeat {
- output = digitNames[number % 10]! + output
- number /= 10
- } while number > 0
- return output
- }
Closures Are Reference Types
Whenever you assign a function or a closure to a constant or a variable, you are actually setting that constant or variable to be a reference to the function or closure.
Escaping Closures
异步解决方案
Autoclosures
表达式语句
- // customersInLine is ["Ewa", "Barry", "Daniella"]
- func serve(customer customerProvider: @autoclosure () -> String) {
- print("Now serving \(customerProvider())!")
- }
- serve(customer: customersInLine.remove(at: 0))
- // Prints "Now serving Ewa!"
swift语言点评四-Closure的更多相关文章
- Swift语言指南(四)--类型安全和类型推断
原文:Swift语言指南(四)--类型安全和类型推断 Swift是一门类型安全语言,类型安全语言需要代码里值的类型非常明确.如果你的代码中有部分值需要String类型,你就不能错误地传递Int. 鉴于 ...
- swift语言点评十四-继承
Overriding A subclass can provide its own custom implementation of an instance method, type method, ...
- swift语言点评二
一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...
- swift语言点评一
一.变量定义 1.常量与变量 Use let to make a constant and var to make a variable. 2.类型与推测 However, you don’t alw ...
- swift语言点评十九-类型转化与检查
1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...
- swift语言点评十八-异常与错误
1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...
- swift语言点评十七-Designated Initializers and Convenience Initializers
Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...
- swift语言点评十-Value and Reference Types
结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...
- swift语言点评八-枚举
总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” valu ...
随机推荐
- CommandType.Text
CommandType.Text代表执行的是SQL语句CommandType.StoreProcedure代表执行的是存储过程CommandType代表要执行的类型 //返回DataTable的SQL ...
- 如何创建一个asp页面
Active Server Pages(ASP)文件是以 .asp 为扩展名的文本文件,这个文本文件可以包括下列部分的任意组合: 文本 HTML 标记 ASP 脚本命令 创建 .asp 文件非常容易. ...
- 学Arduino 需要做哪些准备?(引自"知乎用户:郑兴芳,DhP"的回答)
本人非电子专业,使用Arduino完全出于兴趣,目前主要用于实验过程中的自动化操作. 一.基础准备主要是看一些入门介绍的电子文档,如Arduino_Basic.PDF.ArduinoL2.PDF .& ...
- Mac 如何寻找Mac自带的IDLE
Mac 如何寻找Mac自带的IDLE 每次要打开IDLE时,需要如下动作:打开terminal --> 输入idle --> 回车,就自动打开IDLE了 图标如下: 选择在“Finder中 ...
- LayUI中实现上级下拉框动态加载下级下拉框js
js代码: var form = layui.form, layer = layui.layer; form.on("select(上级)", function(data){ va ...
- linux防火墙查看状态firewall、iptable
一.iptables防火墙1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service ...
- HDU 1047 Integer Inquiry( 高精度加法水 )
链接:传送门 思路:高精度水题 /************************************************************************* > File ...
- POJ 2187 Beauty Contest( 凸包求最远点对 )
链接:传送门 题意:给出 n 个点,求出这 n 个点中最远的两个点距离的平方 思路:最远点对一定会在凸包的顶点上,然后直接暴力找一下凸包顶点中距离最远的两个点 /******************* ...
- BZOJ 3672 [NOI2014]购票 (凸优化+树剖/树分治)
题目大意: 略 题面传送门 怎么看也是一道$duliu$题= = 先推式子,设$dp[x]$表示到达$x$点到达1节点的最小花费 设$y$是$x$的一个祖先,则$dp[x]=min(dp[y]+(di ...
- Linux 操作系统启动流程
1.加载bios bios中包含的硬件CPU 内存 硬盘等相关信息 2.读取MBR 读取完bios信息之后,计算机会查找bios制定的硬盘MBR引导扇区,将其内容复制到 0x7c00 地址所在的物理内 ...