Fix UIViewController supportedInterfaceOrientations() to use UIInterfaceOrientationMask and OptionSetType in Swift 2

There are many changes coming in Swift 2 that further unify the Swift language and provide better support and method signatures.

A welcome change is the OptionSetType in Swift 2, this makes it easier to work with settings that relied on bit masks (a complex topic of binary logic) to use a Set and it's common operations.

The automatic conversion utility didn't help fix this change, so I had to fiddle a few times until I figured it out.

You'll see an error for your supportedInterfaceOrientations() method because the method signature has changed again. It now returns a UIInterfaceOrientationMask instead of an Int. 

Error: Method does not override any method from its superclass

While it breaks code now, this change makes Swift easier to use moving forward. Changes to how Objective-C code looks using new macros, generic types, and the OptionSetType will improve how you write code in Swift.

// Swift 1.2
override func supportedInterfaceOrientations() -> Int {
    let orientation = Int(UIInterfaceOrientationMask.Portrait.rawValue | UIInterfaceOrientationMask.PortraitUpsideDown.rawValue)
    return Int(UIInterfaceOrientationMask.All.rawValue)
}

Becomes a bit more verbose and uses the array notation to create a OptionSetType. It seems you need to set the type, otherwise the statement gets interpreted as an Array type, which isn't what you want.

// Swift 2
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Portrait, UIInterfaceOrientationMask.PortraitUpsideDown]
    return orientation
}

This new change helps make Swift 2 code feel more at home, and a little less crazy than the hoops you had to jump through in Swift 1.2 and earlier.

Links