7

I need to create custom extension for my class that depends on iOS version.

So, for instance:

@available(iOS 10.0, *) extension Foo { func test() { print("do something for iOS 10.0 and later") } } 

What if I would execute test for version between 9.0 (included) and 10.0 (excluded)?

2
  • You can't mark an extension with available. Just curious, why not add an availability check in the function? Commented Apr 11, 2017 at 9:13
  • 1
    @ebby94 Because it's not cool 🙂 Commented Jan 25, 2022 at 4:57

1 Answer 1

2

I should appreciate you for asking this question.

@available works similarly to #available in that you specify the iOS release you want to target, and then Xcode handles the rest. For example:

@available(iOS 9, *) func iOS9Work() { // do stuff } 

If your deployment target is iOS 8, you can't call that iOS9Work() method without some availability checking first. You can stack up these checks if you need to, for example:

//You need to perform this check before calling if #available(iOS 9, *) { iOS9Work() } @available(iOS 9, *) func iOS9Work() { // do stuff } 

In your case you have to call like this:

 if #available(iOS 10, *) { Foo() } 

For more info: Availability Checking in Swift

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

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.