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 ...
随机推荐
- Mysql command not found on mac pro
export PATH=${PATH}:/usr/local/mysql/bin If you want this to be run every time you open terminal put ...
- ABBYY FineReader 双十二特惠活动正在进行中...
转眼间11月已悄然飘过,有些童鞋还没缓过双十一的劲,势必将剁手进行到底.只为当时没有鼓足勇气.狠下心来而悔恨其中.别担心,双十二你准备好了么,ABBYY FineReader 系列产品低价让你继续嗨到 ...
- hdu 5691 Sitting in line 状压动归
在本题中,n<=16n<=16n<=16, 不难想到可以将所选数字的编号进行状态压缩. 定义状态 dp[S][j]dp[S][j]dp[S][j],其中 SSS 代表当前所选出的所有 ...
- node——服务器根据不同请求作出不同响应+响应html文件等文件
在浏览器中,不同的请求应该作出不同的响应 我们可以从请求req中的url获得请求的内容 然后我们就可以通过判断请求的url来做响应 代码如下: //根据用户的不同请求,服务器做出不同的响应 // // ...
- Linux 环境中从源代码编译安装 ReText 问题与解决
从源代码编译安装 ReText 问题与解决 1. 如何安装 Python Markups 1.1 从 https://launchpad.net/python-markups 下载 Python Ma ...
- s5k4ba摄像头驱动分析
注释: 本驱动是基于S5PV310的,但是全天下的摄像头驱动都是采用V4L2,因此驱动框架流程基本差不多.其中fimc_init_camera()函数会回调.init函数,该函数主要就是通过IIC总线 ...
- ES6重点知识点总结(2)
ES6重点知识点总结(2) call和apply的作用是什么?区别是什么? call和apply的功能基本相同,都是实现继承或者转换对象指针的作用: 唯一不通的是前者参数是罗列出来的,后者是存到数组中 ...
- Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?
Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别? 1.什么是Set?(what) Set是Collection容器的一个子接口,它不允许出现 ...
- angular-HTTP
AngularJS $http 是一个用于读取web服务器上数据的服务. $http.get(url) 是用于读取服务器数据的函数. <div ng-app="myApp" ...
- Java 获取环境变量
Java 获取环境变量Java 获取环境变量的方式很简单: System.getEnv() 得到所有的环境变量System.getEnv(key) 得到某个环境变量的值 由于某些需要,可能要下载某些 ...