How to conform to NSAccessibilityOutline?

Sadly, I'm not sure that this is actually possible to do in pure Swift, given the definitions laid out in Obj-C.

When I start writing accessibilityRows(), I see two possible overloads to implement:

  • accessibilityRows() -> [NSAccessibilityRow]? comes from the NSAccessibilityTable protocol, which is inherited from NSAccessibilityOutline and is a requirement to fulfill the protocol
  • accessibilityRows() -> [Any]? comes from NSAccessibilityProtocol, which NSView adopts (and which you would need to override)

Because you inherit from both protocols, you'd need to override both methods to conform, but you can't because Swift is a bit more strict about Obj-C's rules than it is.

It also doesn't appear at first glance to be possible to get around using @_implements:

class MyView: NSView, NSAccessibilityOutline { // error: type 'MyView' does not conform to protocol 'NSAccessibilityTable'
    @_implements(NSAccessibilityTable, accessibilityRows)
    func myAccessibilityRows() -> [NSAccessibilityRow]? {
        nil
    }

    @objc
    override func accessibilityRows() -> [Any]? { // note: candidate has non-matching type '() -> [Any]?'
        nil
    }
}

Someone might be able to come up with another creative solution, but this is likely at least worth filing a Radar for.


In the meantime, it may be sufficient for your use-case to write an Objective-C base class which handles the NSAccessibilityOutline protocol implementation, with a concrete Swift subclass with the more "interesting" bits.

1 Like