The speaker mentions there is "less risk of creating a retain cycle in when capturing self in a closure within a value type", such as a SwiftUI view struct.
Can you elaborate on when this could actually occur?
Thanks!
Can you elaborate on when this could actually occur?
Thanks!
You can still create retain cycles indirectly. For example, suppose you write something like:
The closure will capture self, which retains obj, which retains the closure, so this forms a retain cycle. Most examples like this are pretty contrived, which is why we decided to not require self. before bar() in that closure, but they can happen.
Code Block swift class MyObject { var function = { () } } struct MyStruct { let obj = MyObject() func structMethod() { ... } init() { obj.function = { structMethod() } } }
The closure will capture self, which retains obj, which retains the closure, so this forms a retain cycle. Most examples like this are pretty contrived, which is why we decided to not require self. before bar() in that closure, but they can happen.