The Thread Sanitizer, also known as TSan, is an LLVM based tool to audit threading issues in your Swift and C language written code. It was first introduced in Xcode 8 and can be a great tool to find less visible bugs in your code, like data races. At WeTransfer, the Thread Sanitizer helped us …
PassthroughSubject vs. CurrentValueSubject explained
PassthroughSubject and CurrentValueSubject are two types from the Combine framework that conforms to the Subject protocol. Both are very similar but have a few keys differences that are important to know. You can see them as custom publishers, but the main difference is that they are a little easier to work with. If you’re new …
@AppStorage explained and replicated for a better alternative
The @AppStorage Property Wrapper was introduced in SwiftUI to allow easy access to the user defaults. When property wrappers were introduced, many examples used a user defaults wrapper as an example. It’s a common use case where you can move boilerplate code into a wrapper. Although the @AppStorage wrapper is working out great in many …
How to use throwing properties to catch failures in Swift
Throwing properties allow defining computed properties that throw an error on failure. SE-310 introduced this feature in Swift 5.5 and is part of the async-await concurrency changes allowing async properties to throw errors. By defining throwing computed properties, we better handle unhappy flows without defining methods for simple accessors—the same counts for custom subscripts that …
Unwrap or throw: Exploring solutions in Swift
Unwrap or throw is a scenario in which we want to throw an error if an optional returns a nil value. Techniques like if let or guard statements make this easy to do but often return in quite some boilerplate code. In cases like this, I’m always hoping to find a solution I wasn’t aware …
Async let explained: call async functions in parallel
Async let is part of Swift’s concurrency framework and allows instantiating a constant asynchronously. The concurrency framework introduced the concept of async-await, which results in structured concurrency and more readable code for asynchronous methods. If you’re new to async-await, it’s recommended first to read my article Async await in Swift explained with code examples. How …
Development Assets in Xcode to enrich SwiftUI Previews
Development Assets in Xcode allow you to provide test data to use within SwiftUI previews and other code during development. Assets marked for development will only be included in debug builds and removed once you create an archive of your app. Without being aware of development assets, you might have added a few smaller images …
Dependency Injection in Swift using latest Swift features
Dependency Injection is a software design pattern in which an object receives other instances that it depends on. It’s a commonly used technique that allows reusing code, insert mocked data, and simplify testing. An example could be initializing a view with the network provider as a dependency. There are many different solutions for dependency injection …
Actors in Swift: how to use and prevent data races
Swift Actors are new in Swift 5.5 and are part of the big concurrency changes at WWDC 2021. Before actors, data races were a common exception to run into. So before we dive into Actors with isolated and nonisolated access, it’s good to understand what Data Races are and to understand how you can solve …
Flaky tests resolving using Test Repetitions in Xcode
Flaky tests can be frustrating to deal with. You’re ready to open your PR until you realize your tests fail on CI while they succeeded locally. You even realize your test is succeeding when executing individually, and you’re almost at a point to say, “Merging in as tests succeed locally.” Test repetitions sometimes allow you …