Swift 中 navigationControllerSupportedInterfaceOrientations 的 return 值是多少?

What is the return value of navigationControllerSupportedInterfaceOrientations in Swift?

UINavigationControllerDelegate的委托方法。 Swift 和 Objective-C 中的 return 值不同。见:

Obj-C:

 - (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController

Swift

func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> Int

当我使用 objective-c 时,它工作正常。但是,当我使用 Swift 时,我无法 return 更正方法中的值。见下图:

谁能解释一下为什么,有什么解决办法吗?

尝试使用这个

func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController!) -> Int 
{
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

有关详细信息,请单击此处

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch06p281navigationInterface/ch19p615navigationInterface/ViewController.swift

铸造的原因在Objective-C

上面的函数return类型是NSInteger,因为UIInterfaceOrientationPortrait是NSInteger类型的枚举。

而Swift是以struct的形式定义的

struct UIInterfaceOrientationMask : RawOptionSetType {
init(_ rawValue: UInt)
init(rawValue: UInt)

static var Portrait: UIInterfaceOrientationMask { get }
static var LandscapeLeft: UIInterfaceOrientationMask { get }
static var LandscapeRight: UIInterfaceOrientationMask { get }
static var PortraitUpsideDown: UIInterfaceOrientationMask { get }
static var Landscape: UIInterfaceOrientationMask { get }
static var All: UIInterfaceOrientationMask { get }
static var AllButUpsideDown: UIInterfaceOrientationMask { get }
}

https://books.google.co.in/books?id=5baVBQAAQBAJ&pg=PA277&lpg=PA277&dq=reason+of+UIInterfaceOrientationMask.Portrait.rawValue+using+rawValue&source=bl&ots=3X2wXD9HQc&sig=1Knh1LPBj2aoRMwuYAHUFjGdHxw&hl=en&sa=X&ei=0Z_6VPizEs7VuQTJz4KYBA&ved=0CC8Q6AEwAw#v=onepage&q=reason%20of%20UIInterfaceOrientationMask.Portrait.rawValue%20using%20rawValue&f=false

我认为正确的答案是将 return 类型设置为 UIInterfaceOrientationMask,如下所示:

func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}