Negate #available statement

后端 未结 5 616
南旧
南旧 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条回答
  •  [愿得一人]
    2020-12-09 08:44

    Seems it's the best solution, before Swift2 you had to use other methods such as using ready-to-use classes wrote by individuals. But that's fine, you can set a variable in viewDidLoad() and use it to detect the older devices:

    var isNewerThan8: Bool = false
    
    func viewDidLoad(){
       if #available(iOS 8.0, *) { 
          isNewerThan8 = true
       } else { 
          isNewerThan8 = false
       }
    }
    
    func myFunction(){
       if isNewerThan8 {
         //foo
       }else{
        //boo
       }
    }
    

提交回复
热议问题