Recent articles
Jump to a random postSolving “Capture of non-sendable type in @Sendable closure” in Swift
Published on: August 7, 2024Once 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...Solving “Reference to captured var in concurrently-executing code” in Swift
Published on: July 31, 2024In Xcode 16, this error actually is sometimes presented as "Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure". The cause is the exact same as what's covered in this post. 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 warnings 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...
Read more...Adding values to the SwiftUI environment with Xcode 16’s Entry macro
Published on: July 15, 2024Adding custom values to SwiftUI’s environment has never been very hard to do to. However, the syntax for doing it is verbose and easy to forget. To refresh your mind, take a look at this post where I explain how to add your own environment values to a SwiftUI view. To summarize what’s shown in that post; here’s how you add a custom value to the environment using Xcode 15 and earlier: private struct DateFormatterKey: EnvironmentKey { static let defaultValue: DateFormatter = { let formatter = DateFormatter() formatter.locale = Locale(identifier: "en_US_POSIX") formatter.dateFormat = "MM/dd/yyyy" return formatter }() } extension EnvironmentValues...
Read more...Let and var in Swift explained
Published on: July 12, 2024Virtually every programming language will have some means to define properties; Swift does too. We have two approaches to defining a property in Swift. We can use a var or a let. The code below shows how we can define a var or a let as a member of a class: class Member { let id: UUID var name: String init(name: String) { self.id = UUID() self.name = name } } This class has two properties. One is a let, the other is a var. If you're coming from a Javascript background you might expect that there's a third option...
Read more...Using PreviewModifier to build a previewing environment
Published on: July 10, 2024Xcode 16 and iOS 18 come with a feature that allows us to build elaborate preview environments using a new PreviewModifier protocol. This protocol allows us to define objects that can create a single context or environment that’s cached and used across your SwiftUI previews. This is useful because it means that you could, for example, populate a database with a bunch of mock data that is then used in your previews. You can also use PreviewModifier to apply specific styling to your previews, to wrap them all in a specific wrapper, and more. Essentially, they’re a tool that allows...
Read more...Mixing colors in SwiftUI and Xcode 16
Published on: June 18, 2024SwiftUI in iOS 18 and macOS 15 has gained a new trick; it can mix colors. This means that it’s now possible to take a color and modify it by applying another color to it using a provided percentage. The video below shows how this works: Notice how the large rectangle updates its color to be a certain mix of a left and right color. In the video I use distinct colors but you can also mix with white or black to lighten or darken your color. One use of color mixing I like a lot is to explore color...
Read more...Using iOS 18’s new TabView with a sidebar
Published on: June 12, 2024In iOS 18, Apple has revamped the way that tab bars look. They used to be positioned at the bottom of the screen with an icon and a text underneath. Starting with iOS 18, tab bars will no longer be displayed in that manner. Instead, on iPad you will have your tab bar on the top of the screen with text-only items while on iPhone your tab bar will retain its old look. In addition to changing how a tab bar looks, Apple has also added new behavior to the tab bar; it can expand into a sidebar that contains...
Read more...Building a stretchy header view with SwiftUI on iOS 18
Published on: June 11, 2024In iOS 18, SwiftUI's ScrollView has gotten lots of love. We have several new features for ScrollView that give tons of control to us as developers. One of my favorite interactions with scroll views is when I can drag on a list an a header image animates along with it. In UIKit we'd implement a UIScrollViewDelegate and read the content offset on scroll. In SwiftUI we could achieve the stretchy header effect with GeometryReader but that's never felt like a nice solution. In iOS 18, it's possible to achieve a stretchy header with little to no workarounds by using the...
Read more...Modern logging with the OSLog framework in Swift
Published on: June 7, 2024We all know that print is the most ubiquitous and useful debugging tool in a developer’s toolbox. Sure, we have breakpoints too but what’s the fun in that? Sprinkling some prints throughout our codebase to debug a problem is way more fun! And of course when we print more than we can handle we just add some useful prefixes to our messages and we’re good to go again. What if i told that you can do way better with just a few lines of code. You can send your prints to more places, give them a priority, and more. Of...
Read more...@preconcurrency usage in swift explained
Published on: May 28, 2024When you enable strict concurrency checks for your existing projects, it’s likely that Xcode will present loads of warnings and/or errors when you compile your project for the first time. In this post, I’d like to take a look at a specific kind of error that relates to code that you didn’t write. The @preconcurrency declaration can be added to: functions types protocols imports Let’s take a look at all of these areas to fully understand how @preconcurrency helps us enable strict concurrency checks even if we can’t update all of our dependencies just yet. @preconcurrency imports To be specific,...
Read more...