#if vs @available vs if #available

What are the differences between #if, @available, and if #available, in terms of both compilation and use case?

Here is my understanding so far:

If I want to make a function available on macOS only, I can do it like this:

#if os(macOS)
func someMacOSOnlyFunction() { /* ... */ }
#endif

If I want to restrict it even further to some specific versions, I can do either of these 2:

@available(macOS 10.15, *)
func someMacOS15PlusOnlyFunction() { /* ... */ }
if #available(macOS 10.15, *) {
    func someMacOS15PlusOnlyFunction() { /* ... */ }
}

I know that the compiler ignores everything in #if, if the condition is not met. Does the compilation work likewise for @available and if #available? Does @available behave differently from if #available in any way? What are the best practices for choosing/using them?

1 Like