4

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 ...... } } 

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

As long as you don't test for the mere existence of the property to determine what version number you're running.
@NRitH +1: Yes, definitely don't reinvent #available here with such a property...! ;]
Awesome that works. Yea will stick with the @available attribute then
4

You can wrap it with a computed property, for instance for UIMenu:

@available(iOS 13.0, *) var menu: UIMenu? { // use this as a getter for what you want return _menu as? UIMenu } var _menu: Any? // use this reference as a setter for what you want 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.