我有一个结构体:
struct ErrorResultType: ErrorType { var description: String var code: Int }和协议:
protocol XProtocol { func dealError(error: ErrorResultType) }现在我想扩展UIViewController:
Now I want to make an extention of UIViewController:
extension UIViewController: XProtocol { func dealError(error: ErrorResultType) { // do something } }因此,我可以从中继承该类并覆盖如下功能:
So I can subclass from this and override the function like:
class ABCViewController: UIViewController { --->override func dealError(error: ErrorResultType) { super.dealError(error) // do something custom } }但是它出了错:扩展声明不能被覆盖
对我来说没有任何意义.当我将所有 ErrorResultType 替换为 AnyObject 时,该错误将不再显示.
It doesn't make any sense to me. When I replace all ErrorResultType with AnyObject, the error won't appear any more.
我错过了什么吗?
推荐答案现在,扩展程序中的方法必须标记为 @objc 才能在子类中覆盖它.
For now the method in the extension must be marked with @objc to allow overriding it in subclasses.
extension UIViewController: XProtocol { @objc func dealError(error: ErrorResultType) { // do something } }但是,这要求方法签名中的所有类型都必须与Objective-C兼容,而您的 ErrorResultType 不兼容.但是,将您的 ErrorResultType 设为类而不是结构应该可以.
But that requires all types in the method signature to be Objective-C compatible which your ErrorResultType is not. Making your ErrorResultType a class instead of a struct should work though.
更多推荐
快速覆盖功能错误
发布评论