Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Availability check for OS version range

Tags:

swift

In my app, I want to do things when the OS version is strictly in a specific range, and do something else when outside that range (either below or above it). The specific context is within a SwiftUI @ViewBuilder, but that's immaterial to the version check itself. I know I can accomplish that with something like this, for example:

if #available(iOS 16.4, *) {
    baseView
}
else if #available(iOS 16.0, *) {
    baseView
        .extraModifierOnlyNecessaryBetweeniOS16And16Point4()
}
else {
    baseView
}

This applies the extra modifier only when the iOS version is in the range 16.0..<16.4, which is what I want. However, it's not very DRY, since I have to enumerate baseView for both the case where it's above and below that range, even though those cases are exactly equivalent

I was hoping I'd be able to do this, but it doesn't compile, as Swift says that #available and #unavailable can't be used in the same statement:

if #available(iOS 16, *), #unavailable(iOS 16.4) {
    baseView
        .extraModifierOnlyNecessaryBetweeniOS16And16Point4()
}
else {
    baseView
}

Is there a better way to accomplish this so that I don't have to handle the above and below cases separately?

like image 643
Michael Hulet Avatar asked Jun 28 '26 20:06

Michael Hulet


1 Answers

Maybe you can do this:

    if #available(iOS 16, *) {
        if #unavailable(iOS 16.4) {
            return baseView
                .extraModifierOnlyNecessaryBetweeniOS16And16Point4()
        }
    }
    return baseView
like image 186
biao wzb Avatar answered Jul 04 '26 21:07

biao wzb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!