Created
August 7, 2021 05:05
-
-
Save JarvisTheAvenger/5d8540eab3165b4f7e9e1d5c62159d2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Closures | |
// Escaping closures vs Non Escaping closures | |
// Escaping closures escapes the function body | |
func doSomething(_ a: Int, completion: @escaping (Int) -> Void) { | |
var a = a | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { | |
a += 10 | |
completion(a) | |
} | |
completion(a) | |
} | |
doSomething(2) { value in | |
print(value) | |
} | |
func createSomething(_ a: Int, completion: ((Int) -> Void)?) { | |
var a = a | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { | |
a += 10 | |
completion?(a) | |
} | |
completion?(a) | |
} | |
// Trailing closure | |
createSomething(2) { value in | |
print(value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment