Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify minimum macOS version for Swift Package Manager with Swift 5

In order to have some code compile with SwiftPM, without adding #if available, I'm building the project with the following parameters:

swift build -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.11"

Swift Package Manager also works with Xcode .xcconfig files, but only when generating an Xcode project.

Is there an easy way in Swift 5 to specify the minimum version of macOS when building from the command line with swift build?

Compiler error example:

error: 'archivedData(withRootObject:)' is only available on OS X 10.11 or newer
        let data = NSKeyedArchiver.archivedData(withRootObject: value)
like image 214
Eneko Alonso Avatar asked Apr 03 '19 17:04

Eneko Alonso


People also ask

Does Swift package manager support iOS?

The Swift Package Manager can be used to add external libraries in Xcode projects. This makes the tool a good alternative to Cocoapods and Carthage for managing the dependencies of an iOS project.

How do I add a dependency to a Swift package?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.

How do I use Swift package manager Xcode 13?

With Xcode 13, you need to add packages through the new menu they introduced with the Swift Package Collections. To find this menu, you need to navigate to your project settings where you can find a new menu for Swift Packages. From here you can click the + button to add new packages.


1 Answers

let package = Package(
    name: "NAME",
    platforms: [
        .macOS(.v10_11)
    ],
    products: [
        .library(name: "NAME", targets: ["NAME"]),
    ],
    targets: [
        .target(name: "NAME"),
    ]
)

One way to do this is with Deployment Settings in SPM.

like image 118
Alex Avatar answered Oct 19 '22 05:10

Alex