OGeek|极客世界-中国程序员成长平台

标题: ios - 扩展隐藏了我想要访问的属性。解决方法? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:55
标题: ios - 扩展隐藏了我想要访问的属性。解决方法?

我正在使用两个 pod:DropDownSwiftyUtils .

DropDown 添加了一个名为 DropDownUIView 子类。 DropDown 类定义了自己的 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?



Best Answer-推荐答案


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

DropDown中的widthCGFloat?类型,但是SwiftyUtils<中的width/code> 是 CGFloat 类型。这意味着如果我传递一个可选的 CGFloat,编译器会理解我的意思是 DropDown 中的 width

所以不要这样做:

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

我这样做了:

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

关于ios - 扩展隐藏了我想要访问的属性。解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42174748/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4