Aml*_*xer 6 uiview ios swift swift-extensions
我试图通过扩展我的一个自定义UIViews来调用方法,但我得到错误"类型'MyCustomView'的值没有成员'testMethod'".以下是我的代码
extension MyCustomView {
func testMethod() {
//do stuff here
}
}
//in a separate class from the extension
class func onMoreOptionsButtonPressed(currentViewController:UIViewController) {
for view in currentViewController.view.subviews {
if view.isKindOfClass(MyCustomView) {
let myCustomView = view as! MyCustomView
myCustomView.testMethod()
}
}
}
Run Code Online (Sandbox Code Playgroud)
显然我可以通过一些不同的方式实现这个功能,但是我更感兴趣的是为什么这个代码不能编译,因为它在逻辑上对我来说是正确的.非常感谢所有帮助.
这可能与 UIView 是一个 Objective-C 类以及方法调度与纯 Swift 类略有不同有关。
您可能必须使用动态指令声明扩展的方法以满足 Obj-C 的方法分派机制。
(我还没试过,请注意)
| 归档时间: |
|
| 查看次数: |
646 次 |
| 最近记录: |