tomahh tomahh - 28 days ago 20
iOS Question

Negate #available statement

If want to execute a code block only on devices running with an OS older than

iOS8
. I can't do:

if #available(iOS 8.0, *) == false {
doFoo()
}


The solution I'm using for now is:

if #available(iOS 8.0, *) { } else {
doFoo()
}


, but it feels clunky. Is there another way to negate the
#available
statement elegantly with swift2 ?

Answer

It is not possible to have logic around the #available statement.

Indeed, the statement is used by the compiler to infer what methods can be called within the scope it embraces, hence nothing can be done at runtime that would conditionally execute the block.

It is possible though to combine conditions, using a comma, as follows

if #available(iOS 8.0, *), myNumber == 2 {
  // some code
}