ios – Swift is there an easy way to mark a superclass getter/setter as unavailable


So I have the following code:

public final class MyAdaptiveSprite: SKSpriteNode {

  private let textureColor: AdaptiveColor
  
  public init(texture: SKTexture, size: CGSize, color: AdaptiveColor) {
    textureColor = color
    super.init(texture: texture, color: .clear, size: size)
    adaptThemeChange()
  }

  @available(*, unavailable)
  public override var color: UIColor {
    // Still needs implementation, since `super.init` calls it. 
    // Can't use fatalError, or will crash
    get { return super.color }
    set { super.color = newValue }
  }

  public func adaptThemeChange() {
    // Have to call super, since we mark `color` as unavailable
    super.color = textureColor.lightModeOrDarkModeColor
  }
}

This code is a bit ugly, I simply want to hide color property, because I want my caller to only use adaptThemeChange() API, and not accidentally use color setter. However, I have to implement the whole setter/getter, which is tedious. Is there a more elegant way of marking an API as unavailable?

Another problem with this approach is that I can’t use self.color within MyAdaptiveSprite anymore, because it’s marked as unavailable including MyAdaptiveSprite itself. Is there a way to mark it unavailable to external callers, but not the class itself? (It’s fine if it’s not possible to do so).

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img