0

I'm trying to make a class conform to a protocol by extending that class. In my case I'd like to conform SKNode to UIAccessibilityIdentification.

UIAccessibilityIdentification has only one requirement, that is the property accessibilityIdentifier, which is an optional string. XCode automatically suggests me the following stub with a setter and getter.

public var accessibilityIdentifier: String? {
    get {
        //code
    }
    set(accessibilityIdentifier) {
        //code
    }
}

My question:

How do I implement the getter and setter correctly to make it work? (Subclassing is not an option as I need to use this on several built-in subclasses of SKNode)

At the moment I'm using the following setup which assigns the accessibilityLabel of the SKNode superclass to the accessibilityIdentifier and it works fine for the moment as I'm only UI testing and I don't use voiceover for the user yet. However this might change in the future. So a proper, direct implementation would be better.

My current extension:

import SpriteKit

extension SKNode: UIAccessibilityIdentification {
    
    public var accessibilityIdentifier: String? {
        get {
            super.accessibilityLabel
        }
        set {
            super.accessibilityLabel = newValue
        }
    }
}
Marco Boerner
  • 1,243
  • 1
  • 11
  • 34
  • I'm lost on the problem. Why does your current extension not work on all cases of `SKNode`? – impression7vx Feb 08 '21 at 21:54
  • It does work just fine, however with my extension at the moment the accessibilityLabel is equal to the accessibilityIdentifier But accessibilityIdentifier is meant for UI testing only, and accessibilityLabel is outward facing to the user for voiceover features which can be localized too. I don't think it's a good idea to use the same text for both as one speaks to the developer and the other to the user. : ) – Marco Boerner Feb 08 '21 at 22:00

0 Answers0