Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with non-escaping closures in Swift 3

Tags:

closures

swift

I have an extension Array in the form of:

extension Array
{
    private func someFunction(someClosure: (() -> Int)?)
    {
        // Do Something
    }

    func someOtherFunction(someOtherClosure: () -> Int)
    {
        someFunction(someClosure: someOtherClosure)
    }
}

But I'm getting the error: Passing non-escaping parameter 'someOtherClosure' to function expecting an @escaping closure.

Both closures are indeed non-escaping (by default), and explicitly adding @noescape to someFunction yields a warning indicating that this is the default in Swift 3.1.

Any idea why I'm getting this error?

-- UPDATE -- Screenshot attached: enter image description here

like image 690
XmasRights Avatar asked Dec 19 '22 08:12

XmasRights


1 Answers

Optional closures are always escaping.

Why is that? That's because the optional (which is an enum) wraps the closure and internally saves it.

There is an excellent article about the quirks of @escaping here.

like image 170
Sulthan Avatar answered Jan 09 '23 14:01

Sulthan