How to use Result in Swift

How to use Result in Swift

SE-0235 introduced a Result type into the standard library, giving us a simpler, clearer way of handling errors in complex code such as asynchronous APIs. This is something folks have been asking for since the very earliest days of Swift, so it's great to see it finally arrived in Swift 5!

Swift’s Result type is implemented as an enum that has two cases: success and failure. Both are implemented using generics so they can have an associated value of your choosing, but failure must be something that conforms to Swift’s Error type. If you want, you can use a specific error type of your making, such as NetworkError or AuthenticationError, allowing us to have typed throws for the first time in Swift, but this isn't required.

[…]

Sponsor Hacking with Swift and reach the world's largest Swift community!

When you first meet Result it’s common to wonder why it’s useful, particularly when Swift already has a perfectly good throws keyword for handling errors ever since Swift 2.0.

[…]

You might also want to try out my What’s new in Swift 5.0 playground, which lets you try Swift 5’s new features interactively.

[…]