Visualizing section snapshot structure makes easy!

How I Created a DSL for Diffable Section Snapshot using Result Builders

This article is originally published at https://swiftsenpai.com on March 26, 2021.

If you are like me, have been using a diffable data source section snapshot for quite some time, I am sure you will notice that the code to construct a section snapshot is actually quite difficult to reason about. The append(_:) and append(_:to:) API doesn't really show us the hierarchical data structure it represents.

With the release of result builders in Swift 5.4, it makes me wonder is it possible to create a domain-specific language (DSL) that can help us to:

  1. Construct a section snapshot easily.
  2. Visualize the section…


Say goodbye to data races

Darth Vader
Photo by Tommy van Kessel 🤙 on Unsplash

Data races — the worst nightmare of all developers! They are hard to detect, very unpredictable, and extremely difficult to fix. Apple has given developers various toolsets such as NSLock and serial queues to prevent data races from happening during runtime, however, none of them are capable of catching race conditions during compile-time. With the release of Swift 5.5, this will no longer be the case!

Introducing Actor, the new Swift language feature that can help developers to catch any possible race conditions during development time. In this article, we will first look at how a data race occurs when…


Power up your iOS apps with Swift concurrency

Neon lines
Photo by Federico Beccari on Unsplash

Traditionally, when we want to make a network request, we must use the closure-based URLSession APIs to perform the request asynchronously so that our apps can be responsive while waiting for it to complete. With the release of Swift 5.5, this is no longer the case, we now have another alternative which is to use async/await.

In this article, I would like to show you how to make a network request by using the async/await keywords. …


A quick overview of async/awaits, structured concurrency, and actor

Photo by frank mckenna on Unsplash

Writing asynchronous code has always been a challenging task for developers. Throughout the years, Apple has provided various tools such as grand central dispatch (GCD), Operations, and dispatch queue that help developers in writing asynchronous code. All these tools are great, but each of them has its own pros and cons.

In this year’s WWDC, Apple brought that to the next level and introduced Swift concurrency, built-in language support that promises asynchronous code that is simple to write, easy to understand, and the best of all, free from race conditions.

In this article, I would like to give you a…


Don’t use UserDefaults for storing data. Use Keychains instead

Photo by Vinit Vispute on Unsplash

When developing an iOS app, at times we need to store sensitive data (password, access token, secret key, etc) locally. For junior developers, the first thing that comes to mind will be storing it using UserDefaults.

However, as we all know, storing sensitive data using UserDefaults is a very bad idea, it is because data stored using UserDefaults is not encrypted and extremely insecure.

In order to securely store sensitive data locally, we should use the keychain service provided by Apple. …


Real-world examples of map, reduce, filter, and more

Architecture
Photo by Clark Van Der Beken on Unsplash.

As a developer, very often we need to deal with complex algorithms that take hours or even days to develop. Thanks to Swift’s higher-order functions such as map, reduce, filter, etc., some of those complex algorithms can now easily be solved with just a few lines of code.

In this article, I would like to show you five algorithms that used to be difficult to implement but are now extremely easy to achieve thanks to the higher-order functions in Swift.

Throughout this article, I will be using the following students array as the model object so that you can have…


Reload improvements are quite promising, but there is a catch when using them on value-type model objects

Drawing of phone screen

In October 2020, I published an article that discusses how to reload a table and collection view cell when using a diffable data source. The article shows you how to use two totally different approaches when reloading cells with reference- and value-type items.

As a recap, for reference-type items, we can leverage the snapshot’s reloadItems(_:) method to reload the specified items. For value-type items, reloadItems(_:) will not work. We will have to manually replace the updated items within the snapshot.

I was always confused by why Apple makes such a simple task so complicated. At WWDC21, Apple showed us another…


Let’s juice up that LLDB debugger!

Drawing of code on monitor
Image by the author.

If I asked iOS developers which LLDB command they use the most, they would probably answer po. But did you know that you can actually define your own custom LLDB command using purely Swift code?

In this article, I will show you what it takes to create your own LLDB command. Here is what I will be covering:

  • Adding your first LLDB command
  • Adding an LLDB command with arguments
  • Converting complex Swift code to an LLDB command

Let’s get started!

Adding Your First LLDB Command

Understanding the LLDB command structure

In order to add a custom LLDB command, we must leverage the command alias LLDB command. …


No third-party Swift library required

Graphic showing the words “Automatic keyboard avoidance” with a keyboard and the word UIKit

In iOS 14, Apple shows their love to SwiftUI by giving it automatic keyboard avoidance. It’s turned on by default, meaning all your SwiftUI views can get this awesome feature automatically once you build your apps for iOS 14.

Unfortunately, for whatever reason, Apple does not make this available for UIKit despite the fact that it is one of the features most requested by developers around the world.

In this article, I would like to show you how to enable automatic keyboard avoidance in a view controller using a trick that I recently discovered. …


A keypath can cause havoc!

Crying emoji

This is a story about me spending a whole hour just to remove rounded corners from an UIImageView. I’m sure my fellow developers will have had a similar experience at some point — spending an unreasonable amount of time to complete a simple task. This is one of those stories.

This story does not involve any extraordinary programming knowledge, but I do like to use it to bring out some of my views regarding the attitude that developers should have when solving problems. …

Lee Kah Seng

https://swiftsenpai.com | iOS developer since 2011 | Swift Lover | Music Addict | Anime Fan | Part-time Backpacker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store