Search code examples
macosswift4appkit

NSAccessibilityRadioButton implementation in Swift 4


I'm trying to make an NSAccessibilityRadioButton element, but I'm getting ... let's say an unexpected error. Here is what I'm doing

  1. I have a base class of NSAccessibilityElement:

    class AccessibilityElementPrototype: NSAccessibilityElement {
      .
      .
      .
    }
    
  2. I have another class, which should implement NSAccessibilityRadioButton protocol like this:

    class AccessibilityElementTab: AccessibilityElementPrototype, NSAccessibilityRadioButton {
        func accessibilityValue() -> NSNumber? {
            ...
        }
    }
    

The problem is that I'm getting the following error:

Method 'accessibilityValue()' with Objective-C selector 'accessibilityValue' conflicts with method 'accessibilityValue()' from superclass 'NSAccessibilityElement' with the same Objective-C selector

If someone has already encountered such a problem and has solution, please share.


Solution

  • I'm really happy you asked this, because I thought I was crazy.

    It appears, unbelievably, that some of the NSAccessiblity protocols are broken in Swift. I've opened a bug with Apple, and I encourage you do the same. In the mean time, it's fairly easy to work around this, just inconvenient.

    You can directly use NSView methods to achieve the same result:

    view.setAccessibilityRole(...)
    view.setAccessibilityElement(true)
    
    @objc override func accessibilityValue() -> Any? {
        ...
    }
    

    Don't forget about setting the accessibiltyElement property to true. I didn't realize that was necessary at first, and couldn't figure out why nothing was showing up. Accessibility Inspector's Show Ignored Elements helped me there.