How to add a required protocol property to an extension?

259 views Asked by At

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
        }
    }
}
0

There are 0 answers