If #available check failing

I had thought that the check worked like Objective C's "respondsToSelector" which would work regardless of the build / target sdk. But it seems like #available does not guard against building with an sdk version where those symbols don't exist. ie it's a run-time vs. a build time check.

Yes, if #available is only a runtime check; it doesn't change the fact that the compiler still needs to type check the code inside the then block. When building against the 16.2 SDK, there's no property declaration that the compiler can use to understand how to compile the statement self.isInspectable = true. Even in Objective-C, a respondsToSelector check wouldn't work on its own; you would also have to forward declare the isInspectable property in your project so that the compiler has a definition of the selector for the getter. There is no equivalent to forward declaration in Swift (at least for arbitrary kinds of declarations).

There have been some previous discussions of language features that could be added to Swift to address this problem, mostly revolving around allowing developers to conditionalize code at build time based on either the version of the SDK or the version of a specific module.

2 Likes