Search code examples
swift

Default swift build to modern platforms?


I'm really not enjoying having to put @available(macOS 12, *) all over the place. Is there a way to configure Swift to just build the code with the assumption that it will work?

For example, in the screenshot below, I have already added the annotation twice, but it seems to require a third annotation just to satisfy the LSP (and then presumably the compiler).

I really don't care if whatever it's complaining about was first available in macOS versions I don't intend to target. I just want the code to compile for the latest OS.

LSP warning


Solution

  • To solve this, add a platforms block to the Package.swift file. So, if the Package.swift file is:

    // swift-tools-version:6.0
    
    import PackageDescription
    
    let package = Package(
        name: "Example",
    
        products: [
            .library(name: "Example", targets: ["Example"])
        ],
    
        targets: [
            .target(name: "Example", dependencies: []),
            .testTarget(name: "ExampleTests", dependencies: ["Example"]),
        ]
    )
    

    Update it to:

    // swift-tools-version:6.0
    
    import PackageDescription
    
    let package = Package(
        name: "Example",
    
        // This definition let's us skip adding `@available(macOS 12, *)`
        // throughout the project.
        platforms: [
            .macOS(.v12)
        ],
    
        products: [
            .library(name: "Example", targets: ["Example"])
        ],
    
        targets: [
            .target(name: "Example", dependencies: []),
            .testTarget(name: "ExampleTests", dependencies: ["Example"]),
        ]
    )