Carpsen90 Carpsen90 - 1 year ago 194
Swift Question

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?

Answer Source

Like this:

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {

...and the rest as you have it.

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.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download