ios 扩展中的声明无法覆盖 Swift 4 中的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44616409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declarations in extensions cannot override yet error in Swift 4
提问by J. Doe
I have an extension:
我有一个扩展:
public extension UIWindow {
override public func topMostController()->UIViewController? { ... }
}
but for my topMostController
I get the next error:
但对我来说,topMostController
我得到了下一个错误:
Declarations in extensions cannot override yet error
It works well for Swift 3.1, but for Swift 4 I get this error. How can it be fixed? What did they change in Swift 4?
它适用于 Swift 3.1,但对于 Swift 4,我收到此错误。如何修复?他们在 Swift 4 中有什么变化?
回答by Sulthan
It will work if you make the base implementation @objc
. See Hamish's answerfor a detailed explanation about the internals.
如果您进行基本实现,它将起作用@objc
。有关内部结构的详细说明,请参阅Hamish 的回答。
Overriding methods declared in extensions is a bit tricky to do correctly. Objective-C supports it, but it's not absolutely safe. Swift aims to do it better. The proposal is not completed yet.
覆盖扩展中声明的方法有点难以正确执行。Objective-C 支持它,但它不是绝对安全的。Swift 的目标是做得更好。该提案尚未完成。
Current version of the proposal available here.
可在此处获得该提案的当前版本。
回答by Nikhlesh Bagdiya
Extensions can add new functionality to a type, but they cannot override existing functionality.
扩展可以向类型添加新功能,但它们不能覆盖现有功能。
Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling).
扩展向现有类、结构、枚举或协议类型添加新功能。这包括扩展您无法访问原始源代码的类型的能力(称为追溯建模)。
Extensions in Swift can:
Swift 中的扩展可以:
- Add computed instance properties and computed type properties
- Define instance methods and type methods
- Provide new initializers
- Define subscripts
- Define and use new nested types
- Make an existing type conform to a protocol
- 添加计算实例属性和计算类型属性
- 定义实例方法和类型方法
- 提供新的初始化程序
- 定义下标
- 定义和使用新的嵌套类型
- 使现有类型符合协议
You are trying to do is similar to what done by this code:
您正在尝试执行的操作类似于此代码所做的操作:
class MyClass: UIWindow {
func myFunc() {}
}
extension MyClass {
override func myFunc() {}
}
NOTE:If you want to override topMostController()
then make subclass of UIWindow
注意:如果你想override topMostController()
创建子类UIWindow
回答by nahung89
Swift 5
斯威夫特 5
Actually there are few problems in OP code:
其实OP代码有几个问题:
UIView
(which is superclass ofUIWindow
) doesn't have methodtopMostController()
, that why you can't override it.Apple doesn't encourage
override func
insideextension
:Extensions can add new functionality to a type, but they cannot override existing functionality.
Incase you still want to override function in extension, there are 2 ways:
UIView
(它是 的超类UIWindow
)没有 methodtopMostController()
,这就是为什么你不能覆盖它。Apple不鼓励
override func
内部extension
:扩展可以向类型添加新功能,但它们不能覆盖现有功能。
如果您仍然想在扩展中覆盖函数,有两种方法:
[A]Mark your function with @objc dynamic func
in parent class:
[A]@objc dynamic func
在父类中标记你的函数:
class Vehicle {
@objc dynamic func run() { /* do something */ }
}
class Car: Vehicle { }
extension Car {
override func run() { /* do another thing */ }
}
[B]Override function from build-in classes, which is descendant of NSObject
.
[B]覆盖内置类的函数,它是NSObject
.
extension UIWindow {
// UIWindow is a descendant of NSObject, and its superclass UIView has this function then you can override
override open func becomeFirstResponder() -> Bool {
...
return super.becomeFirstResponder()
}
}