Last active
March 1, 2019 01:59
-
-
Save paulz/73c2ef148e3a060447c8f2045630fdec to your computer and use it in GitHub Desktop.
use functions for closures
This file contains hidden or 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 UIKit | |
| import Siesta | |
| class LoginOrSignupViewController: UIViewController { | |
| @IBAction func logIn() { | |
| let user = User(email: emailTextField.text!, password: passwordTextField.text!, name: nameTextField?.text) | |
| api.login(user) | |
| .onSuccess(weak(self, type(of: self).transitionToApp)) | |
| .onFailure(weak(self, type(of: self).showError)) | |
| } | |
| } |
This file contains hidden or 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 UIKit | |
| import Siesta | |
| class LoginOrSignupViewController: UIViewController { | |
| @IBAction func logIn() { | |
| let user = User(email: emailTextField.text!, password: passwordTextField.text!, name: nameTextField?.text) | |
| api.login(user) | |
| .onSuccess { [weak self] in self?.transitionToApp() } | |
| .onFailure { [weak self] error in self?.showError(error) } | |
| } | |
| } |
This file contains hidden or 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
| /** | |
| use instance functions as closures for completion blocks | |
| - Parameters: | |
| - obj: object to reference weakly | |
| - block: object function with arguments to use as a completion with the same arguments | |
| e.g. onFailure(weak(self, type(of: self).onFailure) | |
| */ | |
| public func weak<T: AnyObject>(_ obj: T, _ block:@escaping (T) -> () -> Void) -> () -> Void { | |
| return { [weak obj] in obj.map {block($0)()} } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment