Setting the Swift Language mode for an SPM Bundle – Donny Wals


Once you create a brand new Swift Bundle in Xcode 16, the Bundle.swift contents will look a bit like this:

// swift-tools-version: 6.0
// The swift-tools-version declares the minimal model of Swift required to construct this package deal.

import PackageDescription

let package deal = Bundle(
    identify: "AppCore",
    merchandise: [
        // Products define the executables and libraries a package produces, making them visible to other packages.
        .library(
            name: "AppCore",
            targets: ["AppCore"]),
    ],
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .target(
            name: "AppCore"
        )
    ]
)

Discover how the package deal’s Swift instruments model is ready to six.0. If you’d like your mission to reference iOS18 for instance, you are going to must have you ever Swift instruments model set to six.0. A facet impact of that’s that your package deal will now construct within the Swift 6 language mode. Meaning that you will get Swift’s full suite of sendability and concurrency checks in your package deal, and that the compiler will flag any points as errors.

You may not be prepared to make use of Swift 6.0 in your new packages but. In these instances you’ll be able to both set the Swift instruments model again to five.10 when you’re not utilizing any options from the 6.0 toolchain anyway or you’ll be able to set your package deal’s language mode to Swift 5 whereas preserving the 6.0 toolchain:

// swift-tools-version: 6.0
// The swift-tools-version declares the minimal model of Swift required to construct this package deal.

import PackageDescription

let package deal = Bundle(
    identify: "AppCore",
    platforms: [.iOS(.v18)],
    // ... the remainder of the package deal description
    swiftLanguageModes: [.v5]
)

It is also doable to assign the swift language mode for particular targets in your package deal as an alternative. Here is what that appears like:

targets: [
  // Targets are the basic building blocks of a package, defining a module or a test suite.
  // Targets can depend on other targets in this package and products from dependencies.
  .target(
    name: "AppCore",
    swiftSettings: [.swiftLanguageMode(.v5)]
  )
]

By utilizing the Swift 5 language mode you’ll be able to proceed to jot down your code as traditional till you are prepared to begin migrating to Swift 6. For instance, you would possibly need to begin by enabling strict concurrency checks.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles