Recent articles

Jump to a random post

Solving “Main actor-isolated property can not be referenced from a Sendable closure” in Swift

Published on: January 10, 2025

When you turn on strict concurrency checking or you start using the Swift 6 language mode, there will be situations where you run into an error that looks a little bit like the following: Main actor-isolated property can not be referenced from a Sendable closure What this error tells us is that we're trying to use something that we're only supposed to use on or from the main actor inside of a closure that's supposed to run pretty much anywhere. So that could be on the main actor or it could be somewhere else. The following code is an example...

Read more...

Is 2025 the year to fully adopt Swift 6?

Published on: January 9, 2025

When Apple released Xcode 16 last year, they made the Swift 6 compiler available along with it. This means that we can create new projects using Swift 6 and its compile-time data race protections. However, the big question for many developers is: Is 2025 the right time to adopt Swift 6 fully, or should we stick with Swift 5 for now? In this post, I won’t give you a definitive answer. Instead, I’ll share my perspective and reasoning to help you decide whether adopting Swift 6 is right for you and your project(s). The right answer depends on loads of...

Read more...

Sending vs Sendable in Swift

Published on: December 18, 2024

With Swift 6, we have an entirely new version of the language that has all kinds of data race protections built-in. Most of these protections were around with Swift 5 in one way or another and in Swift 6 they've refined, updated, improved, and expanded these features, making them mandatory. So in Swift 5 you could get away with certain things where in Swift 6 these are now compiler errors. Swift 6 also introduces a bunch of new features, one of these is the sending keyword. Sending closely relates to Sendable, but they are pretty different in terms of why...

Read more...

Mocking a network connection in your Swift Tests

Published on: December 12, 2024

Unit tests should be as free of external dependencies as possible. This means that you want to have full control over everything that happens in your tests. For example, if you're working with a database, you want the database to be empty or in some predefined state before your test starts. You operate on the database during your test and after your test the database can be thrown away. By making your tests not depend on external state, you make sure that your tests are repeatable, can run in parallel and don't depend on one test running before another test....

Read more...

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...