supportedInterfaceOrientations方法不会覆盖其超类中的任何方法 [英] The supportedInterfaceOrientations method doesn't override any method from its superclass

查看:964
本文介绍了supportedInterfaceOrientations方法不会覆盖其超类中的任何方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UIViewController中,此代码:

In a UIViewController, this code:

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

给出错误方法不会覆盖其超类中的任何方法

我使用的是Xcode 8 beta 4,iOS部署目标是9.0,而使用Legacy Swift语言版本设置为No 构建设置

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

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

How could I convert the code above to Swift 3?

推荐答案

像这样:

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {

...其余的就像你拥有它一样。

...and the rest as you have it.

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

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:


  • 更改 func var

删除()

- > 更改为

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

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.

同样受影响的方法是 preferredStatusBarStyle prefersStatusBarHidden shouldAutorotate preferredInterfaceOrientationForPresentation 等等。在Objective-C标题中查找 UIKIT_DEFINE_AS_PROPERTIES

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

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

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

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.

有趣的是,这种变化对现有的Objective-C代码没有直接影响,因为在Objective-C中,新的属性声明, @property(非原子,只读)UIInterfaceOrientationMask supportedInterfaceOrientations; ,通过与以前相同的方法得到满足:

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:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

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

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.

这篇关于supportedInterfaceOrientations方法不会覆盖其超类中的任何方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆