Created
December 13, 2016 10:03
-
-
Save silentsudo/a2ca1f478b390d57eb61c697a62b773d to your computer and use it in GitHub Desktop.
Callback and closure test in playground
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
var hello: () -> String = { | |
return "Hello!" | |
} | |
hello() | |
var doubleValue: (Int) -> Int = { x in | |
return x * x | |
} | |
let double2 = doubleValue(2) | |
print("Double of 2 is \(double2)") | |
// concat name | |
var concatName: (String, String) -> String = { | |
(firstName, lastName)in | |
return firstName + " " + lastName | |
} | |
let fullName = concatName("John", "Doe") | |
print("Full name is \(fullName)") | |
//Callbacks | |
func testCallbackEmpty( callback: @escaping () -> Void) { | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { | |
callback() | |
} | |
} | |
testCallbackEmpty(callback: { () -> Void in | |
DispatchQueue.main.async { | |
print("Hey called here") | |
} | |
print("Hey called here") | |
}) | |
enum Result { | |
case OK, FAILED | |
} | |
func mainCallback(callback: @escaping (Result) -> Void) { | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { | |
callback(Result.OK) | |
} | |
} | |
mainCallback(callback: { result in | |
print("Hurray \(result)") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment