Created
March 17, 2017 14:17
-
-
Save nicnocquee/05899cf47e6bdc062d96dc51f3fc84c1 to your computer and use it in GitHub Desktop.
Why is this not creating strong reference cycles?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class HTMLElement { | |
| let name: String | |
| let text: String? | |
| lazy var asHTML: () -> String = { | |
| [unowned self] in // without this capture list, self is captured strongly in the closure | |
| if let text = self.text { | |
| return "<\(self.name)>\(text)</\(self.name)>" | |
| } else { | |
| return "<\(self.name) />" | |
| } | |
| } | |
| init(name: String, text: String? = nil) { | |
| self.name = name | |
| self.text = text | |
| } | |
| deinit { | |
| print("\(name) is being deinitialized") | |
| } | |
| } | |
| var h2: HTMLElement? = HTMLElement(name: "h2", text: "Hello") | |
| // why don't the following create strong reference cycles? | |
| h2!.asHTML = { | |
| return "Huh \(h2!.name)" // h2 is not captured strongly here? why? | |
| } | |
| print(h2!.asHTML()) | |
| h2 = nil // h2 still calls deinit |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So it seems like it's not strong reference cycle because
h2is optional.OTOH, the following causes strong reference cycle
Without
[unowned h2] in,h2instance is never deallocated because the closure captures it strongly.