Swift 5.5 introduces a new concept called “effectful read-only properties”, which essentially means that computed properties can now utilize contr

Throwing and asynchronous Swift properties

submited by
Style Pass
2021-07-28 13:30:06

Swift 5.5 introduces a new concept called “effectful read-only properties”, which essentially means that computed properties can now utilize control flow mechanisms like errors and async operations when computing their values.

Essential Developer : If you’re a mid/senior iOS developer who’s looking to improve both your skills and your salary level, then join this 100% free online crash course, starting on August 2nd. Learn how to apply truly scalable iOS app architecture patterns through a series of lectures and practical coding sessions.

Let’s start by taking a look at how computed properties can now throw errors using Swift’s standard error handling mechanism. As an example, let’s say that we’re currently using the built-in Result type’s get method to either extract a result’s wrapped value, or throw any error that it contains:

Since that get method doesn’t actually perform any kind of work, but rather just lets us unpack a Result value using the try keyword, it could now just as well be declared as a property — for example like this:

Leave a Comment