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!
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!
Accessing iCloud files
To access files from the file picker correctly, we need to do two things:
- call
`startAccessingSecurityScopedResource()
andstopAccessingSecurityScopedResource()
- wrap reading the files in
NSFileCoordinator.coordinate(with:queue:byAccessor:)
so the access doesn't conflict with another app writing to the file at the same time.
Below is a code example for how to correctly do this:
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!
Member discussion