Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

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

In a UIViewController, this code:

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

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

How could I convert the code above to Swift 3?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • Change func to var

  • Delete ()

  • Change -> to :

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.

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

Implications

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.

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:

- (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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...