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.
相关文章
- Razor syntax reference for ASP.NET Core
- Aspose.Words for .NET使用文档教程(2):克隆文档
- C#【多线程篇】Parallel.For和for的效率比较(Paralle.Foreach和foreach比较类似)
- 解决Incorrect integer value: '' for column 'id' at row 1的方法
- 定时任务 $ ls /etc/cron* + cat$ for user in $(cat /etc/passwd | cut -f1 -d:); do crontab -l -u $user; done
- From ROS to Unity: leveraging robot and virtual environment middleware for immersive teleoperation
- mybatis或hibernate框架连接数据库报错:java.sql.SQLException: No suitable driver或No suitable driver found for http://maven.apache.org
- arcgis for flex全国地图天气预报的具体实现过程解析
- 浅析Nginx配置获取客户端真实IP的proxy_set_header、X-Real-IP、$remote_addr、X-Forwarded-For、$proxy_add_x_forwarded_for分别是什么意思
- Version 1.8.0_201 of the JVM is not suitable for this product. Version: 11 or greater is required.
- delphi vlc 安装bug 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC"
- BSS Audio® Introduces Full-Bandwidth Acoustic Echo Cancellation Algorithm for Soundweb London Conferencing Processors
- python第十二课——for in循环
- Eclipse Key Shortcuts for Greater Developers Productivity--reference
- 40 JavaScript Chart and Graph Libraries for Developers--reference
- 关于VBA的Exit For到底是怎样跳出for循环的小例子
- 关于VBA的Exit For到底是怎样跳出for循环的小例子
- 《Structured Knowledge Distillation for Dense Prediction》论文笔记
- Parallel.For循环与普通的for循环