1、block需要(拷贝)保存;

2、block引用的环境变量需要处理。

相当于oc中的copy block。

Escaping Closures

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.

One way that a closure can escape is by being stored in a variable that is defined outside the function. As an example, many functions that start an asynchronous operation take a closure argument as a completion handler. The function returns after it starts the operation, but the closure isn’t called until the operation is completed—the closure needs to escape, to be called later. For example:

  1. var completionHandlers: [() -> Void] = []
  2. func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
  3. completionHandlers.append(completionHandler)
  4. }

The someFunctionWithEscapingClosure(_:) function takes a closure as its argument and adds it to an array that’s declared outside the function. If you didn’t mark the parameter of this function with @escaping, you would get a compile-time error.

Marking a closure with @escaping means you have to refer to self explicitly within the closure. For example, in the code below, the closure passed to someFunctionWithEscapingClosure(_:) is an escaping closure, which means it needs to refer to self explicitly. In contrast, the closure passed to someFunctionWithNonescapingClosure(_:) is a nonescaping closure, which means it can refer to self implicitly.

  1. func someFunctionWithNonescapingClosure(closure: () -> Void) {
  2. closure()
  3. }
  4. class SomeClass {
  5. var x = 10
  6. func doSomething() {
  7. someFunctionWithEscapingClosure { self.x = 100 }
  8. someFunctionWithNonescapingClosure { x = 200 }
  9. }
  10. }
  11. let instance = SomeClass()
  12. instance.doSomething()
  13. print(instance.x)
  14. // Prints "200"
  15. completionHandlers.first?()
  16. print(instance.x)
  17. // Prints "100"

Escaping Closures 两点:本质是生命周期标示符的更多相关文章

  1. 喜闻乐见-Android应用的生命周期

    本文主要讲述了App的启动流程.Application的生命周期以及进程的回收机制. 在绝大多数情况下,每一个Android应用都在自己的Linux进程中运行.当需要运行某些代码时,进程就会被创建.进 ...

  2. Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?

    Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...

  3. 【译】WebAPI,Autofac,以及生命周期作用域

    说明 原文地址:http://decompile.it/blog/2014/03/13/webapi-autofac-lifetime-scopes/ 介绍 这是一篇关于AutoFac的生命周期作用域 ...

  4. 匹夫细说C#:不是“栈类型”的值类型,从生命周期聊存储位置

    0x00 前言: 匹夫在日常和别人交流的时候,常常会发现一旦讨论涉及到“类型”,话题的热度就会立马升温,因为很多似是而非.或者片面的概念常常被人们当做是全面和正确的答案.加之最近在园子看到有人翻译的& ...

  5. ASP.NET Core中的依赖注入(4): 构造函数的选择与服务生命周期管理

    ServiceProvider最终提供的服务实例都是根据对应的ServiceDescriptor创建的,对于一个具体的ServiceDescriptor对象来说,如果它的ImplementationI ...

  6. Spring Bean的生命周期(非常详细)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  7. React Native 之生命周期

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  8. 连载《一个程序猿的生命周期》-《发展篇》 - 3.农民与软件工程师,农业与IT业

    相关文章:随笔<一个程序猿的生命周期>- 逆潮流而动的“叛逆者”        15年前,依稀记得走出大山,进城求学的场景.尽管一路有父亲的陪伴,但是内心仍然畏惧.当父亲转身离去.准备回到 ...

  9. Activity详解生命周期(Android)

    Activity是Android组件中最基本也是最为常见用的四大组件(Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器)之 ...

随机推荐

  1. ORA-15024: discovered duplicately numbered ASM disk 0

    在尝试删除一个diskgroup的时候遇到这个错误. ORA-15024: discovered duplicately numbered ASM disk 0 这说明oracle认为有两个disk ...

  2. 3.1-HDLC/PPP

    同步串行链路(Serail Point-to-Point Link)的封装 3.1-HDLC/PPP     高级数据链路控制HDLC(High-Level Data Link Control):   ...

  3. 关于new和malloc以及delete和free能否够混用

    /* *1>当申请的空间是内置类型时,delete和free能够混用 *2>当申请的空间是自己定义类型时, *       1>若没有析构函数.delete和malloc能够混用.有 ...

  4. Android studio 解决setText中文乱码问题

    我在用Android Studio编译器的时候,总会遇到非常多乱码的问题.第一个乱码问题是在Layout文件中面定义了EditText.在代码中须要将获取到的内容填充到EditText里面,这时候假设 ...

  5. CoffeeScript的缩进

    CoffeeScript用缩进代替了花括符"{}",作用范围的划分只靠缩进.这带来代码精简.简洁的同时,有时候也让人困惑. 比如说: if 3 > 1 alert(" ...

  6. [Unity UGUI]ScrollRect效果大全

    UGUI各种优化效果 本文所实现的UGUI效果需求如下: - 支持缩放滑动效果 - 支持动态缩放循环加载 - 支持大数据固定Item复用加载 - 支持不用Mask遮罩无限循环加载 - 支持Object ...

  7. apt --fix-broken install

    1 自动修复安装出现broken的package 但是,如果还是失败的话,就需要手动进行干预了.

  8. Linux I2C设备驱动编写(一)【转】

    本文转载自:http://blog.csdn.net/airk000/article/details/21345457 在Linux驱动中I2C系统中主要包含以下几个成员: I2C adapter 即 ...

  9. LaTeX 在线编辑器(LaTeX online editors)

    eqneditor:有强大的几乎所有常用的数学符号对应的图标形式,便于快速完成latex公式编辑且易于粘贴拷贝. 此外,更为重要的一点是,随着编辑窗口内公式的编辑,会在页面的底部,自动生成其对应的 h ...

  10. python pass 的用法

    python pass用法 1.空语句 do nothing 2.保证格式完整 3.保证语义完整 4.以if语句为例: C/C++中写法: if(true) ; // do nothing else ...