我最近将我的代码迁移到了 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.
isValid
在 extension 中声明。
错误几乎是在说,如果以这种方式声明一个函数,它不能被覆盖。
该语句对来自扩展和在扩展均有效。
关于ios - 来自扩展的声明在 Swift 4 中还不能被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564557/