James Joshua Street
James Joshua Street

Reputation: 3409

How to resolve ambiguity between property in extension and property in class?

I made an extension to UIView to add the basic shadow properties so that I could set them using @IBInspectable.

extension UIView {
     var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }
}

However, I now want to use a library that has redefined the property:

class ClassA: UIView {
    @objc public dynamic var shadowRadius = DPDConstant.UI.Shadow.Radius {
        willSet { tableViewContainer.layer.shadowRadius = newValue }
        didSet { reloadAllComponents() }
    }
  }

When I try to call the property I get an "ambiguous use of shadowRadius" message.

I am considering just removing the extension and manually calling layer.shadowRadius since I am no longer using IBInspectable on shadows (seemed to be running slow loading all the IBInspectable properties, so I opted to just set stuff in code).

However, I feel like there ought to be another way to handle this situation, so I thought I would ask here.

Upvotes: 0

Views: 141

Answers (1)

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

Your extension provides this var for all instances of UIView and subclasses. The subclass in the library defines a subclass-specific property of the same name and type. I don’t have the reference but the Swift language documentation says you can’t override an extension’s members with a subclass.

Upvotes: 1

Related Questions