总结:整个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. javascript中封装获取样式属性值的兼容方法

    function getStyle(obj, attr) { if (window.getComputedStyle) { return window.getComputedStyle(obj, nu ...

  2. 【摘录】JAVA内存管理-评估垃圾收集性能的工具

    第七章 评估垃圾收集性能的工具 各种各样的诊断和监视工具可以用来评估垃圾收集性能.本章简要概述他们中的几个.可以通过第九章中的“Tools and Troubleshooting”链接获得更多的信息. ...

  3. 立即调用函数(IIFE)

    定义: IIFE:立即调用的函数表达式,声明函数的同时立即调用这个函数. 语法: IIFE的常用写法:这两种写法的作用相同,只是表现形式不同而已,()只是起了自执行的作用 (function(){.. ...

  4. 使用面向对象技术创建高级 Web 应用程序

    作者: 出处: 使用面向对象技术创建高级 Web 应用程序 来源:开源中国社区 作者:oschina 最近,我面试了一位具有5年Web应用开发经验的软件开发人员.她有4年半的JavaScript编程经 ...

  5. git--客户端管理工具初步使用

    说点废话哈 小白一枚, 今年3月份进入自己的第一家公司, 开始成为前端中的一份子,好在公司里有位和我一同进来的一位老哥带着我,从老哥身上学到的知识不多,(因为和老哥只相处工作了三个月,因为家里的事情, ...

  6. HDU 2049 不容易系列之(4)——考新郎( 错排 )

    链接:传送门 思路:错排水题,从N个人中选出M个人进行错排,即 C(n,m)*d[m] 补充:组合数C(n,m)能用double计算吗?第二部分有解释 Part 1. 分别求出来组合数的分子和分母然后 ...

  7. BZOJ 4016 [FJOI2014]最短路径树问题 (贪心+点分治)

    题目大意:略 传送门 硬是把两个题拼到了一起= = $dijkstra$搜出单源最短路,然后$dfs$建树,如果$dis_{v}=dis_{u}+e.val$,说明这条边在最短路图内,然后像$NOIP ...

  8. 引入拦截器及swagger支持及解决redis无法初始化问题

    Springboot引入拦截器 自定义的拦截器类 Interceptor package cn.zytao.taosir.auth.config; import javax.annotation.Re ...

  9. 【Codeforces Round #482 (Div. 2) C】Kuro and Walking Route

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把x..y这条路径上的点标记一下. 然后从x开始dfs,要求不能走到那些标记过的点上.记录节点个数为cnt1(包括x) 然后从y开始 ...

  10. Mysql 5.7 官方文档翻译

    始于 2017年4月1日-愚人节 1.1 MySQL 5.7 新功能 本章节介绍了MySQL 5.7 新版本中新增.废弃.删除的功能. 在1.5章节 Section 1.5, "Server ...