Is self captured within a nested function
Asked Answered
N

2

34

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)
}
Nyctalopia answered 30/10, 2014 at 23:18 Comment(1)
Learning from the rintaro's answer I wrote a more robust answer hereLoppy
K
39

Unfortunately, only Closures have "Capture List" feature like [weak self]. For nested functions, You have to use normal weak or unowned variables.

func myInstanceMethod() {
    weak var _self = self
    func nestedFunction(result : Bool) {
        _self?.anotherInstanceMethod()
    }

    functionExpectingClosure(nestedFunction)
}
Kocher answered 31/10, 2014 at 7:34 Comment(3)
Is this in official documentation by any chance?Adjutant
Seems this is true. I did a test and the weak from the closure is not passed into the self in the nested function. So much for clean code.Mesolithic
Does not seem to be the case anymore.Strunk
S
-4

Does not seem to be the case anymore. This is valid in swift 4.1:

class Foo {
    var increment = 0
    func bar() {
        func method1() {
            DispatchQueue.main.async(execute: {
                method2()
            })
        }

        func method2() {
            otherMethod()
            increment += 1
        }
        method1()
    }

    func otherMethod() {

    }
}

The question remains: How is self captured ?

Strunk answered 5/6, 2018 at 15:6 Comment(2)
If by "valid" you mean that self is not strongly captured then I can't approve that. In my case I had a closure and I was calling a nested func from within this closure and the class would not be released, i.e. deinit was not called. At least that's what my research/debugging showed me.Breaker
it will be capture strongly hereAnole

© 2022 - 2024 — McMap. All rights reserved.