I am working on iOS app which is having Obj C code as well as Swift. I am migrating my existing Objective C category to swift code. But when I override existing method in swift extension, it is not compiling. Swift extension is working well for new methods but not for overriding existing methods.
我正在开发iOS应用,它有Obj C代码和Swift代码。我正在将现有的Objective - C类别迁移到swift代码中。但是当我重写swift扩展中的现有方法时,它不是编译。Swift扩展适用于新方法,但不适用于覆盖现有方法。
Code:
代码:
extension UIViewController {
public override func shouldAutorotate() -> Bool {
return false
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
}
Error:
错误:
Method 'shouldAutorotate()' with Objective-C selector 'shouldAutorotate' conflicts with previous declaration with the same Objective-C selector
Method does not override any method from its superclass
Method 'supportedInterfaceOrientations()' with Objective-C selector 'supportedInterfaceOrientations' conflicts with previous declaration with the same Objective-C selector
Here, Am I missing anything?
这里,我漏掉了什么吗?
I am using Xcode 7.3.1
and Swift 2.x
我使用的是Xcode 7.3.1和Swift 2.x
EDIT:
编辑:
From below Answers, I got to know that We cant change behaviour of existing method of class at runtime in Swift extension like in Objective C Categories. Here I should make a base class which will override method and I should use all my ViewControllers as child classes from new base class as parent.
从下面的答案中,我了解到我们不能像Objective C类那样在快速扩展的运行时改变现有类方法的行为。在这里,我应该创建一个基类,它将覆盖方法,我应该将所有的ViewControllers作为父类从新的基类中使用。
But in my case, I want to change behaviour of all "shouldAutorotate" methods including 3rd party frameworks UIViewController. In above case I cant force all third party frameworks UIviewControllers to become subclass of my base class. In Objective C i could achieve this.
但在我的例子中,我想改变所有"shouldAutorotate"方法的行为包括第三方框架UIViewController。在上面的例子中,我不能强迫所有第三方框架UIviewControllers变成我基类的子类。在目标C中,我可以做到这一点。
1 个解决方案
#1
7
Swift extensions aren't to be used to override methods declared in the class they're extending – especially for Objective-C classes, that's very much like providing two definitions of the same method in the same class. Imagine seeing a class that looked like:
Swift扩展不能用于覆盖它们正在扩展的类中声明的方法——特别是对于Objective-C类,这很像在同一个类中提供相同方法的两个定义。想象一下,看到一门课
class UIViewController : UIResponder {
public func shouldAutorotate() -> Bool {
return true
}
public func shouldAutorotate() -> Bool {
return false
}
}
Which one wins? That's the conflict you're being warned about.
哪一个赢了?这就是你被警告的冲突。
If you need to override methods for a view controller of yours, you'll need to do that in a subclass, not an extension.
如果需要重写视图控制器的方法,则需要在子类中完成,而不是在扩展中。
Ninja edit: this may have been possible to do in Objective-C, but was a programming error there. If a category duplicates a method from a main class, which definition gets used is undefined. See this SO post and the backing Apple documentation.
Ninja编辑:这在Objective-C中可能实现,但这是一个编程错误。如果一个类别复制了一个来自主类的方法,则使用哪个定义是未定义的。请参阅SO post和支持苹果的文档。
#1
7
Swift extensions aren't to be used to override methods declared in the class they're extending – especially for Objective-C classes, that's very much like providing two definitions of the same method in the same class. Imagine seeing a class that looked like:
Swift扩展不能用于覆盖它们正在扩展的类中声明的方法——特别是对于Objective-C类,这很像在同一个类中提供相同方法的两个定义。想象一下,看到一门课
class UIViewController : UIResponder {
public func shouldAutorotate() -> Bool {
return true
}
public func shouldAutorotate() -> Bool {
return false
}
}
Which one wins? That's the conflict you're being warned about.
哪一个赢了?这就是你被警告的冲突。
If you need to override methods for a view controller of yours, you'll need to do that in a subclass, not an extension.
如果需要重写视图控制器的方法,则需要在子类中完成,而不是在扩展中。
Ninja edit: this may have been possible to do in Objective-C, but was a programming error there. If a category duplicates a method from a main class, which definition gets used is undefined. See this SO post and the backing Apple documentation.
Ninja编辑:这在Objective-C中可能实现,但这是一个编程错误。如果一个类别复制了一个来自主类的方法,则使用哪个定义是未定义的。请参阅SO post和支持苹果的文档。