0

Before iOS 15 I had a custom class SelectableButtonItem: UIBarButtonItem with isSelected property which worked perfectly:

class SelectableButtonItem: UIBarButtonItem {
  var isSelected = false {
    didSet {
      // ... some custom action
    }
  }
}

iOS 15 and Xcode 13 introduced isSelected property in UIBarButtonItem, so for iOS 15 I don't need to declare it anymore.

@available(iOS 15.0, *) allows to declare properties available in iOS 15 and above.

class SelectableButtonItem: UIBarButtonItem {
  @available(iOS 15.0, *)
  override var isSelected: Bool {
    get { super.isSelected }
    set {
      super.isSelected = newValue
      // ... some custom action
    }
  }
}

I can't put if @available ... else here, as it is not allowed at the property declaration level.

How do I declare not overridden isSelected property only for iOS 14 and below?

Denis Bystruev
  • 326
  • 4
  • 11
  • https://stackoverflow.com/questions/64797366/stored-properties-cannot-be-marked-potentially-unavailable-with-available ? Use a internal property that will have the iOS15 value or the old one if needed? – Larme Sep 21 '21 at 16:26
  • Thanks [Larme](https://stackoverflow.com/users/1801544/larme), for a while I renamed my custom property from `isSelected` to `isButtonSelected`. But still would like to know whether there is a way I can continue to use `isSelected` everywhere. – Denis Bystruev Sep 21 '21 at 16:34
  • @DenisBystruev Why can't you just keep the `override` without an `@available` check, and then do `#available` checks within the getter and setter? – George Sep 21 '21 at 16:36
  • @DenisBystruev Btw, use @ so people get a notification from replies - just writing their name doesn't ping them – George Sep 21 '21 at 16:39

0 Answers0