Negate #available statement

后端 未结 5 623
南旧
南旧 2020-12-09 07:49

I 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()         


        
5条回答
  •  萌比男神i
    2020-12-09 08:48

    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
    }
    

提交回复
热议问题