xcode supportedInterfaceOrientations 方法不会覆盖其超类中的任何方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38732948/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:06:49  来源:igfitidea点击:

The supportedInterfaceOrientations method doesn't override any method from its superclass

iosswiftxcode

提问by ielyamani

In a UIViewController, this code:

在 UIViewController 中,此代码:

public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if let mainController = self.mainViewController{
        return mainController.supportedInterfaceOrientations
    }
    return UIInterfaceOrientationMask.all
}

gives the error Method doesn't override any method from its superclass

给出错误 Method doesn't override any method from its superclass

I am using Xcode 8 beta 4 and the iOS deployment target is 9.0, and Use Legacy Swift Language Versionis set to No in the Build Settings

我使用的是 Xcode 8 beta 4,iOS 部署目标是 9.0,并且Use Legacy Swift Language VersionBuild Settings

How could I convert the code above to Swift 3?

如何将上面的代码转换为 Swift 3?

回答by matt

Like this:

像这样:

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {

...and the rest as you have it.

......剩下的就你所拥有的。

The general pattern

一般模式

A lot of Cocoa methods are properties now, so you implement them as override computed variables. So the pattern for moving from seed 3 (or earlier) to seed 4 is:

许多 Cocoa 方法现在都是属性,因此您可以将它们实现为覆盖计算变量。所以从种子 3(或更早)移动到种子 4 的模式是:

  • Change functo var

  • Delete ()

  • Change ->to :

  • 更改funcvar

  • 删除 ()

  • 更改->:

This works because a computed variable has a getter function, so the function you were implementing before simply turns into the getter function. And these are read-only properties, so you won't need a setter.

这是有效的,因为计算变量有一个 getter 函数,所以你之前实现的函数只是变成了 getter 函数。这些是只读属性,因此您不需要设置器。

Similarly affected methods are preferredStatusBarStyle, prefersStatusBarHidden, shouldAutorotate, preferredInterfaceOrientationForPresentation, and many others. Look for UIKIT_DEFINE_AS_PROPERTIESin the Objective-C header.

同样受影响的方法是preferredStatusBarStyleprefersStatusBarHiddenshouldAutorotatepreferredInterfaceOrientationForPresentation,和其他许多人。UIKIT_DEFINE_AS_PROPERTIES在 Objective-C 标头中查找。

Implications

影响

In the longer term, there are other changes you can make. For example, you canadd a setter (dividing your implementation into getand setfunctions), and thus you can turn your implementation into a facade for a stored property. For example:

从长远来看,您还可以进行其他更改。例如,您可以添加一个 setter(将您的实现分为getset函数),因此您可以将您的实现转换为存储属性的外观。例如:

private var _orientations = UIInterfaceOrientationMask.portrait
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    get { return self._orientations }
    set { self._orientations = newValue }
}

So now your code has a way to set this value. If you were returning different values at different times, this could make things a lot cleaner.

所以现在你的代码有办法设置这个值。如果你在不同的时间返回不同的值,这可以让事情变得更清晰。

Further technical notes

更多技术说明

Interestingly, this change has no direct effect on existing Objective-C code, because in Objective-C, the new property declaration, @property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations;, is satisfied by the same method as before:

有趣的是,此更改对现有的 Objective-C 代码没有直接影响,因为在 Objective-C 中,新的属性声明 ,@property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations;通过与以前相同的方法得到满足:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

The reason is that in Objective-C, a @property(readonly)is merely a promise that a corresponding getter method exists, and that's exactly what this method is. But in Swift, the way to write an Objective-C property's getter method is through a property, that is, through an instance variable. So only Swift code is affected by the change: you have to rewrite your methods as properties.

原因是在Objective-C 中,a@property(readonly)只是一个相应的getter 方法存在的承诺,而这正是这个方法的意义所在。但是在 Swift 中,编写 Objective-C 属性的 getter 方法的方式是通过属性,即通过实例变量。因此只有 Swift 代码会受到更改的影响:您必须将方法重写为属性。