Resolving Strong Reference Cycles for Closures
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:
lazy var someClosure: (Int, String) -> String = {
[unowned self, weak delegate = self.delegate!] (index: Int, stringToProcess: String) -> String in
// closure body goes here
}
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:
lazy var someClosure: () -> String = {
[unowned self, weak delegate = self.delegate!] in
// closure body goes here
}
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相关推荐
- weakreference_Java中WeakReference,SoftReference,PhantomReference和Strong Reference之间的区别...
weakreference 很长一段时间以来,WeakReference和SoftReference都已添加到Java API中,但是并不是每个Java程序员都熟悉它. 这意味着在Java中使用Wea ...
- Java中WeakReference,SoftReference,PhantomReference和Strong Reference之间的区别
很长一段时间以来,WeakReference和SoftReference都已添加到Java API中,但是并不是每个Java程序员都熟悉它. 这意味着在Java中使用WeakReference和Sof ...
- java强引用不会被回收_强引用(Strong Reference)-不回收
在Java程序中,最常见的引用类型是强引用(普通系统99%以上都是强引用),也就是我们最常见的普通对象引用,也是默认的引用类型. 当在Java语言中使用new操作符创建一个新的对象,并将其赋值给一个变 ...
- js 内存引用计数_快速内存管理–自动引用计数
js 内存引用计数 In this tutorial, we'll be covering Swift memory management and learn about Automatic Refe ...
- Swift中文教程(十六) 自动引用计数
Swift使用自动引用计数(ARC)来管理应用程序的内存使用.这表示内存管理已经是Swift的一部分,在大多数情况下,你并不需要考虑内存的管理.当实例并不再被需要时,ARC会自动释放这些实例所使用的内 ...
- swift 通知_Swift 闭包无脑加 [weak self] 行不行?
用到 Swift 的闭包的时候,我们经常会加 [weak self]. 但你有没有问过自己,它是不是非加不可? 比如这个场景: class YourViewController {override f ...
- 如何在'纯'Swift中创建弱协议引用(不带@objc)
本文翻译自:How can I make a weak protocol reference in 'pure' Swift (without @objc) weak references don't ...
- Swift 中的内存管理详解
这篇文章是在阅读<The Swift Programming Language>Automatic Reference Counting(ARC,自动引用计数)一章时做的一些笔记,同时参考 ...
- 《Programming with Objective-C》第四章 Encapsulating Data
Designated Initializer 不稳定的传送门 合成属性 Properties don't always have to be backed by their own instance ...
最新文章
- linux系统proc目录进程信息详解
- Linux系统下活用History命令
- oracle字符串提取函数,oracle字符串分割和提取函数定义
- Leetcode 1
- 逻辑运算符和||与(和|)的区别
- MySQL:Innodb DB_ROLL_PTR指针解析
- Flutter实战之(Clubhouse App)
- django - 替换admin的textarea为 富文本
- 用Java开发一个停车场系统
- 语义模型及自然语言处理系统基础算法
- stream常用操作
- java web 网络安全_Java Web中的入侵检测及简单实现
- 教程入门:建立一个完全自动化的交易系统
- 系统无法开始服务器进程。请检查用户名和密码。 (Exception from HRESULT: 0x8000401A)...
- ExtJs6 响应式布局
- 向日葵服务器维护,向日葵远程服务器
- 903C	Boxes Packing
- 自定义按钮实现video暂停和播放的方法
- 黑灰产攻击洪峰来袭,企业如何守住自己的钱袋子?
- TestNG开源插件Arrow介绍