Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a variable for only a specific iOS version in Swift? On the class level

Tags:

ios

swift

Is it possible to achieve this on the Class level of an object? Specifically looking at this to have an app containing WatchConnectivity but also suppporting iOS8

class Test {
    if #available(iOS 9, *) {
        let session: WCSession ......
    }
}
like image 653
Mervyn Ong Avatar asked Oct 17 '25 17:10

Mervyn Ong


1 Answers

You can use a computed static property:

public class Example {  
  public static var answerToEverything: Int {
    if #available(iOS 9, *) {
      return 42
    } else {
      return 0
    }
  }
}

Or, you can consider using @available attribute instead:

public class Example {
  @available(iOS, introduced=1.0)
  public static var answerToEverything: Int = 42
}

where 1.0 is the version of your library (not iOS).

This would be useful if you're more concerned about making sure it's used on iOS and less concerned about the iOS version.

like image 152
JRG-Developer Avatar answered Oct 20 '25 06:10

JRG-Developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!