ios - 来自扩展的声明在 Swift 4 中还不能被覆盖

标签 ios swift swift4 extension-methods

我最近将我的代码迁移到了 Swift 4。我在使用扩展 时遇到了一个问题,即:

Declarations from extensions cannot be overridden yet

我已经阅读了多篇对这个问题进行重新分级的帖子。但是他们都没有接受下面描述的场景:

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 an extension.

isValidextension 中声明。

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

该语句对来自扩展在扩展均有效。

关于ios - 来自扩展的声明在 Swift 4 中还不能被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564557/

相关文章:

iOS 9 StackView inside ScrollView 不填满屏幕宽度

即使使用默认值,Swift 文本标签也为零

uiimagepickercontroller - 使用Swift 4-Xcode 9退出时出现Picker错误消息(在发现扩展时遇到:Error Domain = PlugInKit Code = 13)

ios - 单元测试: Emulate that user has specific calendar

ios - 在 Swift 中为 UIViewController 自定义 UIView

ios - UIButton 状态转换动画不适用于 setEnabled 或 setHighlighted

ios - 如何防止通知设置被重复注册?

ios - 使用私有(private)全局变量作为扩展名 'properties' 是否有代码味道?

ios - 获取字符串的某些部分

ios - 如何突出显示文本(在 UILabel 中)并将其转换为 IOS8 swift 中的可点击链接/按钮