koromiko / Tutorial

We are sample code :)
325 stars 240 forks source link

Why @escaping #1

Open Ricardo1980 opened 6 years ago

Ricardo1980 commented 6 years ago

Hi!

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

Thanks.

RunningSquirrel commented 6 years ago

“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. ”

koromiko commented 6 years ago

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.