Created
January 12, 2017 15:46
-
-
Save lukeredpath/33afd49e371c5bd0ae78fdf1b3e71ec1 to your computer and use it in GitHub Desktop.
Swift result type
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
enum Result<T, ErrorType> { | |
case success(T) | |
case error(ErrorType) | |
} | |
enum MyError: ErrorType { | |
case itFuckedUp | |
} | |
// no longer need unused parameters in completion blocks: | |
func doSomething(completionHandler: () -> (Result<String, NSError>)) { | |
if itWorks { | |
completionHandler(.success("it worked")) | |
} else { | |
completionHandler(.error(.itFuckedUp)) | |
} | |
} | |
doSomething { result in | |
switch(result) { | |
case .success(let result) { | |
} | |
case .error(let error) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment