Created
May 3, 2018 05:11
-
-
Save suprie/bda7ebbbb07f79f348f0c995b6e2c62b to your computer and use it in GitHub Desktop.
Create a method that chained each other. There is a name for this. But i forgot
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 | |
class Test { | |
var username: String? | |
var password: String? | |
var onSuccess: (() -> (Void))? = nil | |
var onFailure: (() -> (Void))? = nil | |
init() { | |
} | |
func login(username: String, password: String) -> Test { | |
self.username = username; | |
self.password = password; | |
return self; | |
} | |
func onSuccess(_ onSuccess:@escaping () -> (Void)) -> Test { | |
self.onSuccess = onSuccess | |
return self | |
} | |
func onFailure(_ onFailure: @escaping () -> (Void)) -> Test { | |
self.onFailure = onFailure | |
return self | |
} | |
func run() { | |
print(self.username ?? "") | |
print(self.password ?? "") | |
self.onFailure?() | |
self.onSuccess?() | |
} | |
} | |
Test().login(username: "test", password: "password") | |
.onSuccess { | |
print("success") | |
}.onFailure { | |
print("failure") | |
}.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment