Callback function cannot be accessed on its own

don't know how to express the problem correctly, so here is the code:

class Foo {
    func foo2(nextCall: ()->()) {
    }

    func foo(nextCall: ()->()) {
        func f1() {
            foo2(f1)
        }
    }
}

      

Concise, but not sure if it has the same type of problem:

class Foo {
    func foo(nextCall: ()->()) {
        func f1() {
            foo(f1)
        }
    }
}

      

Error message: Cannot reference a local function with captures from another local function.

Any ideas how to fix this problem? The provided function is used as a callback function and for some reason I am wondering why the function cannot be used.

+3


source to share


1 answer


This code compiles successfully:

class Foo {
    func foo(nextCall: ()->()) {
        var f1:(()->())!
        f1 =  {
            self.foo(f1)
        }
    }
}

Foo().foo({})

      



Of course this code doesn't do anything. But if you implement on your own it will work as expected.

0


source







All Articles