扩展中的声明不能覆盖Swift 4中的错误

时间:2023-01-23 22:29:01

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中发生了什么变化?

2 个解决方案

#1


47  

It will work if you make the base implementation @objc. See Hamish's answer for a detailed explanation about the internals.

如果你让base实现@objc,它就会起作用。有关内部人的详细解释,请参阅哈米什的回答。

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支持它,但不是绝对安全的。斯威夫特的目标是做得更好。这个建议还没有完成。

Current version of the proposal available here.

建议书的当前版本可以在这里找到。

#2


9  

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:

在迅速扩展可以:

  • 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
  • 使现有类型符合协议

Apple Developer Guide

苹果开发者指南

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

注意:如果您想重写topMostController(),那么创建UIWindow的子类

#1


47  

It will work if you make the base implementation @objc. See Hamish's answer for a detailed explanation about the internals.

如果你让base实现@objc,它就会起作用。有关内部人的详细解释,请参阅哈米什的回答。

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支持它,但不是绝对安全的。斯威夫特的目标是做得更好。这个建议还没有完成。

Current version of the proposal available here.

建议书的当前版本可以在这里找到。

#2


9  

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:

在迅速扩展可以:

  • 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
  • 使现有类型符合协议

Apple Developer Guide

苹果开发者指南

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

注意:如果您想重写topMostController(),那么创建UIWindow的子类