You resolve a strong reference cycle between a closure and a class instance by defining a capture list as part of the closure’s definition. A capture list defines the rules to use when capturing one or more reference types within the closure’s body. As with strong reference cycles between two class instances, you declare each captured reference to be a weak or unowned reference rather than a strong reference. The appropriate choice of weak or unowned depends on the relationships between the different parts of your code.

NOTE

Swift requires you to write self.someProperty or self.someMethod() (rather than just someProperty or someMethod()) whenever you refer to a member of self within a closure. This helps you remember that it’s possible to capture self by accident.

Defining a Capture List

Each item in a capture list is a pairing of the weak or unowned keyword with a reference to a class instance (such as self) or a variable initialized with some value (such as delegate = self.delegate!). These pairings are written within a pair of square braces, separated by commas.

Place the capture list before a closure’s parameter list and return type if they are provided:

  1. lazy var someClosure: (Int, String) -> String = {
  2. [unowned self, weak delegate = self.delegate!] (index: Int, stringToProcess: String) -> String in
  3. // closure body goes here
  4. }

If a closure does not specify a parameter list or return type because they can be inferred from context, place the capture list at the very start of the closure, followed by the in keyword:

  1. lazy var someClosure: () -> String = {
  2. [unowned self, weak delegate = self.delegate!] in
  3. // closure body goes here
  4. }

Weak and Unowned References

Define a capture in a closure as an unowned reference when the closure and the instance it captures will always refer to each other, and will always be deallocated at the same time.

Conversely, define a capture as a weak reference when the captured reference may become nil at some point in the future. Weak references are always of an optional type, and automatically become nil when the instance they reference is deallocated. This enables you to check for their existence within the closure’s body.

NOTE

If the captured reference will never become nil, it should always be captured as an unowned reference, rather than a weak reference.

Resolving Strong Reference Cycles for Closures相关推荐

  1. weakreference_Java中WeakReference,SoftReference,PhantomReference和Strong Reference之间的区别...

    weakreference 很长一段时间以来,WeakReference和SoftReference都已添加到Java API中,但是并不是每个Java程序员都熟悉它. 这意味着在Java中使用Wea ...

  2. Java中WeakReference,SoftReference,PhantomReference和Strong Reference之间的区别

    很长一段时间以来,WeakReference和SoftReference都已添加到Java API中,但是并不是每个Java程序员都熟悉它. 这意味着在Java中使用WeakReference和Sof ...

  3. java强引用不会被回收_强引用(Strong Reference)-不回收

    在Java程序中,最常见的引用类型是强引用(普通系统99%以上都是强引用),也就是我们最常见的普通对象引用,也是默认的引用类型. 当在Java语言中使用new操作符创建一个新的对象,并将其赋值给一个变 ...

  4. js 内存引用计数_快速内存管理–自动引用计数

    js 内存引用计数 In this tutorial, we'll be covering Swift memory management and learn about Automatic Refe ...

  5. Swift中文教程(十六) 自动引用计数

    Swift使用自动引用计数(ARC)来管理应用程序的内存使用.这表示内存管理已经是Swift的一部分,在大多数情况下,你并不需要考虑内存的管理.当实例并不再被需要时,ARC会自动释放这些实例所使用的内 ...

  6. swift 通知_Swift 闭包无脑加 [weak self] 行不行?

    用到 Swift 的闭包的时候,我们经常会加 [weak self]. 但你有没有问过自己,它是不是非加不可? 比如这个场景: class YourViewController {override f ...

  7. 如何在'纯'Swift中创建弱协议引用(不带@objc)

    本文翻译自:How can I make a weak protocol reference in 'pure' Swift (without @objc) weak references don't ...

  8. Swift 中的内存管理详解

    这篇文章是在阅读<The Swift Programming Language>Automatic Reference Counting(ARC,自动引用计数)一章时做的一些笔记,同时参考 ...

  9. 《Programming with Objective-C》第四章 Encapsulating Data

    Designated Initializer 不稳定的传送门 合成属性 Properties don't always have to be backed by their own instance ...

最新文章

  1. linux系统proc目录进程信息详解
  2. Linux系统下活用History命令
  3. oracle字符串提取函数,oracle字符串分割和提取函数定义
  4. Leetcode 1
  5. 逻辑运算符和||与(和|)的区别
  6. MySQL:Innodb DB_ROLL_PTR指针解析
  7. Flutter实战之(Clubhouse App)
  8. django - 替换admin的textarea为 富文本
  9. 用Java开发一个停车场系统
  10. 语义模型及自然语言处理系统基础算法
  11. stream常用操作
  12. java web 网络安全_Java Web中的入侵检测及简单实现
  13. 教程入门:建立一个完全自动化的交易系统
  14. 系统无法开始服务器进程。请检查用户名和密码。 (Exception from HRESULT: 0x8000401A)...
  15. ExtJs6 响应式布局
  16. 向日葵服务器维护,向日葵远程服务器
  17. 903C Boxes Packing
  18. 自定义按钮实现video暂停和播放的方法
  19. 黑灰产攻击洪峰来袭,企业如何守住自己的钱袋子?
  20. TestNG开源插件Arrow介绍

热门文章

  1. 一方包,二方包,三方包的区别
  2. IE10,IE9透明元素无法点击
  3. Springboot注册Filter
  4. OpenCV的AdaptiveThreshold函数
  5. Ubuntu安装配置Python.pyDev
  6. win10笔记本永久删除文件文件怎样恢复
  7. EasyUI 添加tab页(iframe方式)
  8. 你为什么高考会考砸?-论出题侧重点其实是故意的
  9. 一家企业为何使用多家公司的防火墙
  10. datatable和awk获取列名操作