Compile for different macOS versions

I am learning Swift/SwiftUI and working on my first substantial (for me) macOS application. Since I was following current tutorials and WWDC videos, I ended up using some features that are only available in OSX 12. Is there a way to maintain one code base that can be compiled for different target versions? I think I'm looking for something like C/C++'s #ifdef that would include different pieces of code depending on the target OS version.

Mark

Yes there is.

You have @available for a func:

@available(macOS 11.0, *)
func …

and for a piece of code:

if #available(macOS 11, *) {
    print("This code only runs on macOS 11 and up")
} else {
    print("This code only runs on macOS 10 and lower")
}

get much details here for instance: h t t p s : / / w w w.avanderlee.com/swift/available-deprecated-renamed/

Sorry about the poorly formatted comment. I guess you can't include code blocks in comments, and I can't find a way to delete my comment, so here it is again, in a more readable format.

Thank you for your speedy reply, but I must still be missing something. I created a simple macOS app and modified the ContenView struct.

struct ContentView: View {
    var text: String {
        if #available(macOS 12, *) {
            return "Hello, world! OSX12"
        } else {
            return "Hello, world! OSX11"
        }
    }

    var body: some View {
        Text(text)
            .padding()
    }
}
`

But no matter how I set the macOS Deployment Target in the project or deployment target in the target, it always displays Hello, world OSX 12 when I run it.

Thanks

Compile for different macOS versions
 
 
Q