Skip to content

Instantly share code, notes, and snippets.

Created October 20, 2015 02:12
Show Gist options
  • Save anonymous/6dc0fad4d62131248a7d to your computer and use it in GitHub Desktop.
Save anonymous/6dc0fad4d62131248a7d to your computer and use it in GitHub Desktop.
import UIKit
// MARK: Method chaining
class MethodChaining {
func fetchImage() -> Fetch<UIImage> {
let fetch = Fetch<UIImage>()
if let image = UIImage(named: "cat.jpg") {
if let succeed = fetch.succeed {
// never called because its nil
succeed(image)
}
}
return fetch
}
}
class Fetch<T> {
var succeed: (T -> ())?
func onSuccess(succeed : T -> ()) -> Self {
self.succeed = succeed
return self
}
func onFailure(fail : NSError? -> ()) -> Self {
return self
}
}
let example4 = MethodChaining()
example4.fetchImage().onSuccess { image in
print("do something with image")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment