总结:整个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. 又一个设计工具 Framer X Preview

    又一个设计工具 Framer X Preview 所说比 Sketch 和 Figma 都要好用,不知道是不是真的 ~~ To give you a little background, I've b ...

  2. 错误:com.android.builder.packaging.DuplicateFileException: Duplicate files copied

    File2: C:\Users\guoxw\.gradle\caches\modules-2\files-2.1\org.jsoup\jsoup\1.10.3\b842f960942503cf1abb ...

  3. MySQL · 答疑解惑 · 备库Seconds_Behind_Master计算

    背景 在mysql主备环境下,主备同步过程如下,主库更新产生binlog, 备库io线程拉取主库binlog生成relay log.备库sql线程执行relay log从而保持和主库同步. 理论上主库 ...

  4. IntelliJ Idea下Go项目开启Debug调试

    1.新建Go项目,创建入口go文件(Test1.go),随便写点啥,比如: package main import "fmt" func main(){ fmt.Println(& ...

  5. ONGUI->NGUI->UGUI (Unity UI史)

    各GUI的介绍 ONGUI:Unity自带的绘制界面工具,它的成像原理是基于表层的,所以执行效率非常的低,并且没有提供复杂的UI的接口,就算开发者硬着头皮写上去只能让UI的执行效率更低. NGUI:第 ...

  6. Unity5.0 状态机新增的entry/exit

    相关官方日志 https://blogs.unity3d.com/cn/2014/06/26/shiny-new-animation-features-in-unity-5-0/

  7. io框架

    IO流的三种分类方式 1.按流的方向分为:输入流和输出流 2.按流的数据单位不同分为:字节流和字符流 3.按流的功能不同分为:节点流和处理流 (节点流表示的是直接操作节点(例如文件,键盘)的流,例如F ...

  8. face++算法工程实习生面试

    2018-01-11 算法工程实习生  自动化工具链方面 面试的知识点非常仔细,十分检验基本功底 1.自我介绍 2.算法题,leetcode 第一题 两数之和 问python中数组和字典的查找时间复杂 ...

  9. failed to push some refs to 'git@github.com:RocsSun/mytest.git

    Git推送到GitHub仓库失败 使用Git将文件推送至GitHub的远程仓库时,报错failed to push some refs to 'git@github.com:RocsSun/mytes ...

  10. django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: admin

    创建了一个Django项目,且包含一个admin的app,但是在启动Django的是时候抛出了以下异常: Unhandled exception in thread started by <fu ...