How to create/define a compile time flag for a specific iOS version in swift package

Hi,

I want to add a new custom flag in my swift package to conditionally compile files, something like this below.

#if DISABLE_FORMAT
// Do nothing
#else
class Formatter {
  func format() {
     ...
  }
}
#endif

But I want to introduce this flag only for iOS 15+. I tried the below two approaches, but it is not working,

Added using swiftSettings,

.target(
    name: "Localization",
        dependencies: [],
        swiftSettings: [
            .define("DISABLE_FORMAT[sdk=iphoneos15.0]"),
            .define("DISABLE_FORMAT[sdk=iphonesimulator15.0]"),
        ]
)

Added using cSettings,

.target(
    name: "Localization",
    dependencies: [],
    cSettings: [
        .unsafeFlags(
            [
                "-DISABLE_FORMAT[sdk=iphoneos15.0]=1",
                "-DISABLE_FORMAT[sdk=iphonesimulator15.0]=1",
            ], .when(platforms: [.iOS], configuration: .debug)
        ),
        .unsafeFlags(
            [
                "-DISABLE_FORMAT[sdk=iphoneos15.0]=1",
                "-DISABLE_FORMAT[sdk=iphonesimulator15.0]=1",
            ], .when(platforms: [.iOS], configuration: .release)
        ),
    ]
)

I would appreciate any help.

This isn't possible at this point in time.

I haven’t tried yet but depending on what behavior you want exactly, I would look into
@available with the obsoleted parameter. It won’t disable compilation but can prevent usage which could be enough.

@NeoNacho Thanks for the confirmation. I thought I was adding the flags in an incorrect way.

@Ceylo I will try if @available with obsoleted: 15.0 works. Thanks for the reply.

Terms of Service

Privacy Policy

Cookie Policy