Recent articles

Jump to a random post

Testing completion handler based code in Swift Testing

Published on: December 4, 2024

Swift's new modern testing framework is entirely driven by asynchronous code. This means that all of our test functions are async and that we have to make sure that we perform all of our assertions “synchronously”. This also means that completion handler-based code is not as straightforward to test as code that leverages structured concurrency. In this post, we’ll explore two approaches that can be useful when you’re testing code that uses callbacks or completion handlers in Swift Testing. First, we’ll look at the built-in confirmation method from the Swift Testing framework and why it might not be what you...

Read more...

Testing requirements with #require in Swift Testing

Published on: November 28, 2024

In a previous post, I wrote about using the #expect macro to ensure that certain assertions you want to make about your code are true. We looked at testing boolean conditions as well as errors. In this post, I would like to take a look at a macro that goes hand-in-hand with #expect and that is the #require macro. The #require macro is used to ensure that certain conditions in your test are met, and to abort your test if these conditions are not met. The key difference between #expect and #require is that #expect will not cause a failed...

Read more...

Asserting state with #expect in Swift Testing

Published on: November 21, 2024

I don't think I've ever heard of a testing library that doesn't have some mechanism to test assertions. An assertion in the context of testing is essentially an assumption that you have about your code that you want to ensure is correct. For example, if I were to write a function that's supposed to add one to any given number, then I would want to assert that if I put 10 into that function I get 11 out of it. A testing library that would not be able to do that is not worth much. And so it should be...

Read more...

Improving test coverage with parameterized tests in Swift testing

Published on: October 31, 2024

When you subscribe to the practice of test-driven development or just writing tests in general you'll typically find that you're going to be writing lots and lots of tests for pretty much everything in your codebase. This includes testing that varying inputs on the same function or on the same object result in expected behavior. For example, if you have a function that takes user input and you want to make sure that you validate that a user has not entered a number greater than 100 or smaller than 0, you're going to want to test this function with values...

Read more...

Swift Testing basics explained

Published on: October 23, 2024

Swift testing is Apple's framework for running unit tests in a modern and more elegant way than it was with XCTest, which came before it. This post is the first one in a series of posts that will help you start using Swift Testing in your projects. In this post, we'll take a look at the following topics: Adding a Swift Testing to an existing project Writing your first Swift test Understanding Swift Testing syntax Let's go ahead and dive right in and see what it takes to add a new Swift test to an existing project. Adding a Swift...

Read more...

Testing completion handler APIs with Swift Testing

Published on: October 16, 2024

The Swift testing framework is an incredibly useful tool that allows us to write more expressive tests with convenient and modern APIs. This is my first post about Swift Testing, and I’m mainly writing it because I wanted to write about something that I encountered not too long ago when I tried to use Swift testing on a code base where I had both async code as well as older completion handler based code. The async code was very easy to test due to how Swift Testing is designed, and I will be writing more about that in the future....

Read more...

What is dependency injection in Swift?

Published on: October 11, 2024

Code has dependencies. It’s something that I consider universally true in one way or another. Sometimes these dependencies are third party dependencies while other times you’ll have objects that depend on other objects or functionality to function. Even when you write a function that should be called with a simple input like a number, that’s a dependency. We often don’t really consider the small things the be dependencies and this post will not focus on that at all. In an earlier post, I’ve written about using closures as dependencies, also known as protocol witnesses. In this post I’d like to...

Read more...

Getting started with Mesh Gradients on iOS 18

Published on: September 25, 2024

With iOS 18, we have the possibility to create mesh gradients. Mesh gradients are a really nice way to create very cool UI effects. In this post, we're going to explore exactly what mesh gradients are, how we can use them, and how we can even animate them to look really cool. We’ll start off looking at how we can create a mesh gradient. We're going to take a look at how it more or less works, and then we'll also look at what we can animate and how we can play with mesh gradients. At the end of the...

Read more...

Animating SF Symbols on iOS 18

Published on: September 4, 2024

Over the years, Apple has been putting tons of work into the SF Symbols catalog. With SF Symbols, we’re able to leverage built-in iconography that will look familiar to users while also fitting into the Apple ecosystem very nicely. The fact that there’s thousands of symbols to choose from makes it a highly flexible and powerful catalog of icons that, in my opinion, should be every designer and developer’s first choice when they’re looking for visual components to add to their apps. Initially, SF Symbols were pretty much static. We could configure them with a color and thickness but that...

Read more...

Solving “Value of non-Sendable type accessed after being transferred; later accesses could race;”

Published on: August 23, 2024

Once you start migrating to the Swift 6 language mode, you'll most likely turn on strict concurrency first. Once you've done this there will be several warings and errors that you'll encounter and these errors can be confusing at times. I'll start by saying that having a solid understanding of actors, sendable, and data races is a huge advantage when you want to adopt the Swift 6 language mode. Pretty much all of the warnings you'll get in strict concurrency mode will tell you about potential issues related to running code concurrently. For an in-depth understanding of actors, sendability and...

Read more...