天道酬勤,学无止境

capture-list

如何从方法的闭包中删除强引用循环?(How to remove strong reference cycle from closure from method?)

问题 这里我有一些关闭强引用循环的例子。 如果我为一个存储属性分配一个闭包,我可以使用一个闭包捕获列表来使捕获的引用成为无主/弱引用。 但是,如果我将方法分配给存储属性闭包或将方法分配给外部作用域中的闭包,则无法使用捕获列表。 在最后两种情况下,我可以做些什么来删除参考循环? 使用仅带闭包的捕获列表创建和避免强引用循环的示例 internal class ClosureClass { internal let p1: String internal lazy var p2: () -> String = { [unowned self] // if you comment this out there is a strong reference cycle () -> String in return self.p1 } internal init() { self.p1 = "Default value of ClosureClass" } deinit { print("Object with property '\(self.p1)' is being deinitialized") } } print("Test 'Closure with strong reference to self':") var cc: ClosureClass? = ClosureClass

2021-06-25 10:28:54    分类:技术分享    methods   closures   swift3   capture-list   reference-cycle

是否在嵌套函数中捕获自我(Is self captured within a nested function)

问题 对于闭包,我通常将[weak self]附加到我的捕获列表中,然后对 self 进行空检查: func myInstanceMethod() { let myClosure = { [weak self] (result : Bool) in if let this = self { this.anotherInstanceMethod() } } functionExpectingClosure(myClosure) } 如果我使用嵌套函数代替闭包,我如何对self执行空检查(或者检查甚至是必要的......或者使用这样的嵌套函数是一种很好的做法)即 func myInstanceMethod() { func nestedFunction(result : Bool) { anotherInstanceMethod() } functionExpectingClosure(nestedFunction) } 回答1 不幸的是,只有闭包具有“捕获列表”功能,例如[weak self] 。 对于嵌套函数,您必须使用普通的weak变量或unowned变量。 func myInstanceMethod() { weak var _self = self func nestedFunction(result : Bool) { _self

2021-06-12 17:36:37    分类:技术分享    swift   memory-management   weak-references   nested-function   capture-list

Is self captured within a nested function

With closures I usually append [weak self] onto my capture list and then do a null check on self: func myInstanceMethod() { let myClosure = { [weak self] (result : Bool) in if let this = self { this.anotherInstanceMethod() } } functionExpectingClosure(myClosure) } How do I perform the null check on self if I'm using a nested function in lieu of a closure (or is the check even necessary...or is it even good practice to use a nested function like this) i.e. func myInstanceMethod() { func nestedFunction(result : Bool) { anotherInstanceMethod() } functionExpectingClosure(nestedFunction) }

2021-05-17 14:07:45    分类:问答    swift   memory-management   weak-references   nested-function   capture-list

How to remove strong reference cycle from closure from method?

Here I have some examples for closure strong reference cycles. If I assign a closure to a stored property, I can use a closure capture list to make the captured reference unowned/weak. But if I assign a method to a stored property closure or assign the method to a closure in the outer scope I can not use a capture list. What can I do to remove the reference cycle in the last two cases? Example to create and avoid strong reference cycle with capture list with closure only internal class ClosureClass { internal let p1: String internal lazy var p2: () -> String = { [unowned self] // if you

2021-04-05 07:30:29    分类:问答    methods   closures   swift3   capture-list   reference-cycle