tomahh tomahh - 1 year ago 123
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 ?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download