在Swift 4中不能覆盖扩展中的声明 [英] Declarations from extensions cannot be overridden yet in Swift 4

查看:52
本文介绍了在Swift 4中不能覆盖扩展中的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将代码迁移到了 Swift 4 .我遇到的是扩展名的问题,即:

I have recently migrated my code to Swift 4. There is an issue that I am facing with extensions, i.e.:

扩展中的声明不能被覆盖

Declarations from extensions cannot be overridden yet

我已经阅读了多个帖子,以解决此问题.但是它们都没有满足下面描述的情况:

I have already read multiple posts regrading this issue. But none of them entertains the scenario described below:

class BaseCell: UITableViewCell
{
    //Some code here...
}

extension BaseCell
{
    func isValid() -> String?
    {
        //Some code here...
    }
}

class SampleCell: BaseCell
{
    //Some code here...

    override func isValid() -> String? //ERROR..!!!
    {
        //Some code here...
    }
}

根据苹果公司

扩展可以为类型添加新功能,但不能覆盖现有功能.

Extensions can add new functionality to a type, but they cannot override existing functionality.

但是在上述情况下,我没有覆盖扩展中的方法 isValid().它在 SampleCell 类定义本身中被覆盖.仍然,它给出了错误.

But in the above scenario, I am not overriding the method isValid() in extension. It is overridden in the SampleCell class definition itself. Still, it is giving the error.

推荐答案

但是在上述情况下,我没有在扩展中覆盖方法 isValid().

isValid 扩展名中声明.

错误几乎表明,如果以这种方式声明一个函数,则它不能被覆盖.

The error pretty much says that if a function is declared this way, it cannot be overridden.

该语句对于扩展名中的 和扩展名中的都是有效的.

The statement is valid for both from an extension and in an extension.

这篇关于在Swift 4中不能覆盖扩展中的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆