Warning when using #available below the Deployment Target?

I think it would be a good idea for the compiler to at least warn if you use #available and provide a version that is less than the deployment target. For example if the deployment target is iOS 10, and you have:

if #available(iOS 8.0, *) {
    print("hi")
}

Then a warning (or error?) will show on the first line, saying that iOS 8.0 is less then the deployment target.
To be honest I've not though through this too much, the main thought was to have some way of specifying code that is only relevant to an older version (specifically KVO observers need to be removed in deinit in iOS 10 but not iOS 11). That way in a year or two when we increase the deployment version to iOS 11, because most people are on iOS 11 and 12, we know which code can be removed.

2 Likes

We actually had that warning at one point, but got complaints that it made it harder to work with playgrounds, which always have a deployment target of "your current OS". (In particular, the programming style of "prototype in a playground, then move to framework" and "copy out of framework to inspect more closely".)

5 Likes

After discussing it with a coworker more, the above wouldn't work as I had imagined, because the code could still be needed on future versions.

So a better check would be to allow something other than * in the second parameter, eg:

if #available(*, iOS 8.*) {
    print("hi")
}

To say that the code should only be run up to iOS 8, then when the deployment target is increased past 8, a warning can be produced that the code will never execute.