Skip to content

Instantly share code, notes, and snippets.

@suprie
Created May 3, 2018 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suprie/bda7ebbbb07f79f348f0c995b6e2c62b to your computer and use it in GitHub Desktop.
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
//: 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