tomahh tomahh - 2 months ago 39
iOS Question

Negate #available statement

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

. I can't do:

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

The solution I'm using for now is:

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

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


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