快速覆盖功能错误

编程入门 行业动态 更新时间:2024-10-28 03:26:30
本文介绍了快速覆盖功能错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个结构体:

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.

更多推荐

快速覆盖功能错误

本文发布于:2023-11-26 04:38:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   快速   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!