Skip to content

Instantly share code, notes, and snippets.

@paulz
Last active March 1, 2019 01:59
Show Gist options
  • Select an option

  • Save paulz/73c2ef148e3a060447c8f2045630fdec to your computer and use it in GitHub Desktop.

Select an option

Save paulz/73c2ef148e3a060447c8f2045630fdec to your computer and use it in GitHub Desktop.
use functions for closures
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))
}
}
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) }
}
}
/**
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