ios – supportedInterfaceOrientations方法不会覆盖其超类中的
在UIViewController中,此代码:
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { if let mainController = self.mainViewController{ return mainController.supportedInterfaceOrientations } return UIInterfaceOrientationMask.all } 给出错误方法不会覆盖其超类中的任何方法 我使用的是Xcode 8 beta 4,iOS部署目标是9.0,在Build Settings中将Use Legacy Swift Language Version设置为No 如何将代码转换为Swift 3? 解决方法
喜欢这个:
override var supportedInterfaceOrientations : UIInterfaceOrientationMask { …和其他的,因为你有它。 一般模式 现在很多Cocoa方法都是属性,因此您可以将它们实现为覆盖计算变量。所以从种子3(或更早)到种子4移动的模式是: >将func更改为var 这个工作原理是因为一个计算变量有一个getter函数,所以你刚刚实现的函数只是变成getter函数。这些都是只读属性,所以你不需要一个setter。 类似的受影响的方法是preferredStatusBarStyle,prefersStatusBarHidden,shouldAutorotate,preferredInterfaceOrientationForPresentation等等。在Objective-C头文件中查找UIKIT_DEFINE_AS_PROPERTIES。 启示 从长远来看,还有其他的变化可以做。例如,您可以添加一个setter(将实现划分为get和set函数),因此您可以将实现转换为存储属性的外观。例如: private var _orientations = UIInterfaceOrientationMask.portrait override var supportedInterfaceOrientations : UIInterfaceOrientationMask { get { return self._orientations } set { self._orientations = newValue } } 所以现在你的代码有一个方法来设置这个值。如果您在不同的时间返回不同的值,这可能会使事情变得更加清洁。 进一步的技术说明 有趣的是,这种变化对现有的Objective-C代码没有直接的影响,因为在Objective-C中,新的属性声明@property(非原子,只读)UIInterfaceOrientationMask supportedInterfaceOrientations;通过与以前相同的方法来满足: - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 原因是在Objective-C中,一个@property(只读)只是一个存在相应的getter方法的承诺,这正是这个方法。但是在Swift中,写入Objective-C属性的getter方法的方法是通过一个属性,即通过一个实例变量。所以只有Swift代码受到更改的影响:您必须将方法重写为属性。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |