WOOT! Supercharge Result Type using Swift’s get()
Cope with those results
Swift’s result type has been a tricky one for many Swift programmers, and it has got to the point where many programmers are used to using it. But what if you could supercharge your work by using get() to return the value if a successful value exists, and throws an error otherwise.
Before we start
Difficulty: Beginner | Easy | Normal | Challenging
Video link
Here is a link to the YouTube guide
Prerequisites:
- It would be much easier if you had understanding of Swift’s result type
- .get uses try, so some knowledge of that would be useful
Keywords and Terminology:
JSON: JavaScript Object Notation, a lightweight format for storing and transporting data Swift: An open source programming language for macOS, iOS, watchOS and tvOS
The Background
Calling a network
You might well use a ViewModel to make a network call, and then use a completion closure to retreive the result. To do so, you may well choose to use a result type such as (Result<Users, Error>) -> Void
.