Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created January 12, 2017 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeredpath/33afd49e371c5bd0ae78fdf1b3e71ec1 to your computer and use it in GitHub Desktop.
Save lukeredpath/33afd49e371c5bd0ae78fdf1b3e71ec1 to your computer and use it in GitHub Desktop.
Swift result type
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