总结:整个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的更多相关文章

  1. Swift语言指南(四)--类型安全和类型推断

    原文:Swift语言指南(四)--类型安全和类型推断 Swift是一门类型安全语言,类型安全语言需要代码里值的类型非常明确.如果你的代码中有部分值需要String类型,你就不能错误地传递Int. 鉴于 ...

  2. swift语言点评十四-继承

    Overriding A subclass can provide its own custom implementation of an instance method, type method, ...

  3. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  4. swift语言点评一

    一.变量定义 1.常量与变量 Use let to make a constant and var to make a variable. 2.类型与推测 However, you don’t alw ...

  5. swift语言点评十九-类型转化与检查

    1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...

  6. swift语言点评十八-异常与错误

    1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...

  7. swift语言点评十七-Designated Initializers and Convenience Initializers

    Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...

  8. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  9. swift语言点评八-枚举

    总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” valu ...

随机推荐

  1. linux拷贝文件

    linux之cp/scp命令+scp命令详解 2011-03-09 17:27:22|  分类: Linux |  标签:linux  cp  scp  commond   |字号 订阅   名称:c ...

  2. shell编程-1.字符截取命令-列截取awk+printf

  3. Eclipse本地创建新的GIT分支,并推送至远程Git分支

    本地创建新的GIT分支: 1.右击要创建新分支的项目——Team——Switch To——New Branch…: 2.在弹出的对话框中name框中输入要创建的分支名称,(如果是当前显示的要拷贝的分支 ...

  4. Someing-About-Work

    投料分拨次评估1.调试线投料分波次:(由调试厂录入生产计划:按照实际装机计划做二次分拣)1)计划组必须改两次工单:因为使用的是不同的任务号,一次一次分拣,一次二次分拣,2)计划核对数量困难,不能按照制 ...

  5. [spoj] HIGH - Highways (生成树计数)

    传送门 输入格式: 第一行一个整数T,表示测试数据的个数 每个测试数据第一行给出 n,m 分别表示点数与边数 接下来 m 行,每行给出两个数 a,b ,表示 a,b 之间有一条无向边 输出格式: 每个 ...

  6. [AtCoder Regular Contest 083] Bichrome Tree

    树形DP. 每个点有两个属性:黑色点的权值和,白色点权值和,一个知道另一个也一定知道. 因为只要子树的和它相等的点得权值和不超过x[u],u点的权值总能将其补齐. 设计状态f[u]表示以u为根的子树, ...

  7. LeetCode 856 递归思路详解

    题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) 得 2 * A 分,其中 A 是平衡括 ...

  8. debian系统包管理工具aptitude

    注意:aptitude与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不同的是,aptitude在处理依赖问题上更佳一些.举例来说,aptitu ...

  9. ansible 定义主机用户和密码

    定义主机组用户和密码 [webservers] ansible[01:04] ansible_ssh_user='root' ansible_ssh_pass='AAbb0101' [root@ftp ...

  10. swoole 清除定时器提示no timer

    首页确定一个核心概念 clearTimer仅可清除当前进程的定时器 server代码如下: <?php class Server { private $serv; private $timer; ...