扩展隐藏了我想要访问的 属性。解决方法?

An extension hides a property that I want to access. Workarounds?

我正在使用两个 pods:DropDown and SwiftyUtils

DropDown 添加一个名为 DropDownUIView 子 class。 DropDown class 定义了它自己的 width 属性。客户端代码必须使用此 属性 设置下拉菜单的宽度,而不是设置 frame。它是这样定义的:

public var width: CGFloat? {
    didSet { setNeedsUpdateConstraints() }
}
另一方面,

SwiftyUtils 为所有 UIView 添加了一个扩展。在扩展中,还有一个 width 属性 。 这个 width 属性 只是返回 frame.width 这样人们就可以少写代码了。它是这样定义的:

public var width: CGFloat {
    get { return frame.width }
    set { frame = frame.with(width: newValue) } // frame.with() is defined in SwiftyUtils as well
}

当我尝试使用 DropDwon 中定义的 width 属性 设置 DropDown 的菜单宽度时出现问题。编译器认为我的意思是 SwiftyUtils 模块中扩展中定义的 width 属性

如何告诉编译器我的意思是DropDown中的width,而不是SwiftyUtils中的width

我用一个小技巧解决了这个问题。

DropDown中的widthCGFloat?类型,但是SwiftyUtils中的widthCGFloat类型。这意味着如果我传递可选的 CGFloat,编译器将理解我指的是 DropDown.

中的 width

所以不要这样做:

let menuWidth = <insert calculation here>
menu.width = menuWidth

我这样做了:

let menuWidth = <insert calculation here>
menu.width = menuWidth as CGFloat?