Tutorial icon indicating copy to clipboard operation
Tutorial copied to clipboard

Why @escaping

Open Ricardo1980 opened this issue 6 years ago • 2 comments

Hi!

I wonder why these closures are escaping https://github.com/koromiko/Tutorial/blob/master/MVVMPlayground/MVVMPlayground/Service/APIService.swift

Thanks.

Ricardo1980 avatar Mar 15 '18 20:03 Ricardo1980

“Escaping Closures A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.”

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 4).” iBooks. ”

RunningSquirrel avatar Mar 22 '18 11:03 RunningSquirrel

hi, thank you @tbfyniiswcy for explaining 👍 Yes, I created this class only for simulating network requests, which is usually executed on the background thread (and it is in the example). Thus, we must use @escaping. Otherwise, the code won't even compile.

koromiko avatar Mar 22 '18 13:03 koromiko