Introduction

The Result type is an enumeration with two cases — .success and .failure. Each case has an associated value — the result of a successful operation or an error. Using a Result type can make your code more readable and maintainable because it’s clear what constitutes success and failure. Your code is more robust because you must handle errors.

Although Result is often used in completion handlers for asynchronous functions, you can also use it for synchronous operations. Result can be especially useful when you want to store or pass the result of an operation. Task has a Result type property, and Result has an init(catching:) initializer that lets you capture the outcome of any throwing expression.

Learning Objectives

Here’s what you’ll learn in this lesson:

  • How to use the Result type to capture and handle the outcome of operations that can fail.
  • How to access the result property of a Task.
  • How to convert a throwing expression to a Result.
  • How to write a custom completion handler that returns a Result type.
See forum comments
Download course materials from Github
Previous: Conclusion Next: Result Type