Enabling Concurrency warnings in Xcode 16
Published on: September 13, 2022If you want to make sure that your code adopts Swift concurrency as correctly as possible in Swift 5.x, it's a good idea to enable the Strict Concurrency Checking (SWIFT_STRICT_CONCURRENCY
) flag in your project.
To do this, select your project's target and navigate to the Build Settings
tab. Make sure you select All
from the list of settings that is shown (Basic
is the default) and type Strict Concurrency
in the searchbar to find the Strict Concurrency Checking
build setting.
The screenshot below shows all the relevant parts for you to see:
The default value for this setting is Minimal
which boils down to the Compiler only checking explicit Sendable
annotations amongst other things. This setting is the least restrictive and enforces as little of Swift Concurrency's constraints as possible for the time being.
You can bump your checking to Targeted
which will enforce Sendable
and actor-isolation checks in your code, and it will explicitly verify that Sendable
constraints are met when you mark one of your types as Sendable
. This mode is essentially a bit of a hybrid between the behavior that's intended in Swift 6, and what's allowed now. You can use this mode to have a bit of checking on your code that uses Swift Concurrency without too much warnings and / or errors in your current codebase.
With Complete
you will get the full suite of concurrency constraints, essentially as they will work in Swift 6. Personally I would recommend enabling this setting for new projects where you want all of your code to be properly checked immediately. In an existing codebase this mode might be a little too strict, but on the other hand it will flag lots of things that will be mandatory in Swift 6.
Enabling strict concurrency for Swift Packages
When you're working with Swift packages, you won't be able to turn on strict concurrency through the UI. Instead, you'll have to update your Package.swift
as follows:
let package = Package(
name: "AppCore",
platforms: [.iOS(.v17)],
products: [
.library(
name: "AppCore",
targets: ["AppCore"]),
],
targets: [
.target(
name: "AppCore",
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
)
]
)
Notice the following lines that I passed to my target:
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
Adding this setting will make your package compile with strict concurrency checks enabled.
If you're working on a Swift Package created with Xcode 16, you might have to set your Swift language version back to Swift 5 if you're not ready for Swift 6 yet.
Update: At the moment, it looks like Apple will allow developers to continue using Sendability checking from Swift 5 with the Swift 6 compiler for a very long time. To me, this says that there's still some gaps and complexities in getting all codebases to be compliant with strict Sendable checks.