3 min read

Issue #65

Hi there, welcome to the 65th issue of iOS Code Review! Today I have six learnings to share, let's dive in 👇

Type-driven design

In issue #62 I shared an article from Swiftology talking about a pattern called TypeState, where types are used to restrict code from moving to incorrect states. Now there's more in that article series.:

Type-safe validation: instead of checking if an email is valid and deciding whether to proceed with logic, what if a value of Email struct can only be constructed if the email matches the requirements?

Type-safe access control: similarly, instead of a Bool controlling access to a certain feature, what if a feature flag was a struct that can only be constructed if allowed? This "proof" value can then be passed along to views that are only allowed to exist if the feature is enabled.

These are really cool approaches. Get inspired to look at the type system in a new light!

Type-safe validation | Swiftology
Validation is a very common task in software engineering. We need to validate stuff all the time: user inputs, data formats, array bounds, external configurations, etc. In this article I will show you how to perform validation in a type-safe way, eliminating a whole class of bugs.
Witness pattern — type-safe access control | Swiftology
A common challenge in software design is ensuring that access to a certain functionality is restricted under specific preconditions. In this article, I will show you how to design bulletproof type-safe access control using the **Witness Pattern**.

Verifying associated domains

I remember the pain to verify that associated domains are configured correctly in the app and on the website. Now there's a built in tool:

Combining SF symbols

Check out the video in this tweet - I had no idea it's possible to combine two SF symbols into one by drag and dropping them together. And if you're not yet using the SF Symbols app, here it is

Using inout

Back to code! inout can make Swift code even more reusable - take a look at this example of updating local variables. When I first used inout in a complex code, it wasn't obvious that didSet on the variable was called at the end of the function that takes it as inout. Something to keep in mind!

The power of inout parameters
In Swift, inout parameters can reduce code duplication and the amount of code needed to perform certain tasks. Let’s take a look.

Accessing iCloud files

To access files from the file picker correctly, we need to do two things:

Below is a code example for how to correctly do this:

Setapp offers a collection of 240+ trustworthy, advanced apps under one simple subscription. You will find apps such as Ulysses, Paste, Asset Catalog Creator Pro, DevUtils, Craft, Structured, and many more.
Try Setapp for free

✌️
Alright, that's it for today! Let's spread the good code vibes ✨🧘🌈☀️
I'm curious if you found any of the tips particularly interesting - let me know by replying to this email!