I am having an issue regarding closure escaping.The closure is defined as escaping but somehow the compiler always gives an error.
The error i get is
Closure use of non-escaping parameter 'completion' may allow it to escape.
I have also mentioned the places where i get the error.
Following is the code where closure is defined.
final internal class AnimationTemplate {
var template: ((CompletionFunc) -> AnimationFunc)
init(template: @escaping (CompletionFunc) -> AnimationFunc)
{
self.template = template
}
func perform(with completion: CompletionFunc) { template(completion)() }
}
internal typealias CompletionFunc = (( Bool) -> Void)
internal typealias AnimationFunc = (() -> Void)
Now it is used here.
AnimationQueue.shared.enqueue(animation: AnimationTemplate { completion in
//Error here
//Closure use of non-escaping parameter 'completion' may allow it to escape
return {
(0 ..< self.placeholderViews.count).forEach {
let current: PasscodeSignPlaceholderView = self.placeholderViews[$0]
let state: PasscodeSignPlaceholderView.State = $0 < inputLength ? .active : .inactive
//Error here
//Closure use of non-escaping parameter 'completion' may allow it to escape
current.setState(to: state, with: completion)
}
}
})
I have seen a lot of other questions regarding this error issue but none seems to solve my problem.Any help will be highly appreciated.Thanks
Making the closure optional solved the issue.It was an eventual hit and trial.
internal typealias CompletionFunc = (( Bool) -> Void)?
I saw this closure was passed to a function which accepts an optional closure.Then I decided to make it optional.When i made it optional the error was gone. And xcode was giving me a wrong error perhaps.