Created
October 20, 2015 02:12
-
-
Save anonymous/6dc0fad4d62131248a7d to your computer and use it in GitHub Desktop.
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
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