xcode Swift 2.0 中的 supportedInterfaceOrientations() 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32505938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
supportedInterfaceOrientations() error in Swift 2.0
提问by Danny Bravo
I just migrated a project to Swift 2.0 and this previously working code now produces an error:
我刚刚将一个项目迁移到 Swift 2.0,这个以前工作的代码现在产生了一个错误:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue) | Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
The error indicates the return types are incorrect but I've tried several ways of returning this with no luck.
该错误表明返回类型不正确,但我尝试了多种返回类型但没有成功。
Cannot convert return expression of type 'Int' to return type 'UIInterfaceOrientationMask'
无法将“Int”类型的返回表达式转换为“UIInterfaceOrientationMask”类型的返回类型
回答by Danny Bravo
As of Swift 2.0, bit masking is replaced by using arrays of mask values, your code should now read:
从 Swift 2.0 开始,位掩码被使用掩码值数组替换,您的代码现在应该是:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [.Portrait, .PortraitUpsideDown]
}
回答by alexmorhun
There is an update of supportedInterfaceOrientations for Swift 3.0:
Swift 3.0 的 supportedInterfaceOrientations 有更新:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
And function for autorotating:
和自动旋转功能:
override var shouldAutorotate: Bool {
return true
}
回答by zpasternack
Danny Bravo's answer is correct, but I wanted to point out one other thing. As of Swift 2.0, UIInterfaceOrientationMask
(and most other bitmask types) conform to OptionSetType
, which conforms to AlgebraSetType
, meaning you can do all kinds of operations on those like union
, intersect
, etc.
Danny Bravo 的回答是正确的,但我想指出另一件事。由于雨燕2.0的UIInterfaceOrientationMask
(和大多数其他类型的位掩码)符合OptionSetType
,符合AlgebraSetType
,这意味着你可以做各种操作对那些像union
,intersect
等等。
For example, in Swift 1.2, code like this:
例如,在 Swift 1.2 中,代码如下:
override func supportedInterfaceOrientations() -> Int {
var orientations = UIInterfaceOrientationMask.Portrait
if FeatureManager.hasLandscape() {
orientations |= UIInterfaceOrientationMask.Landscape
}
return Int(orientations.rawValue)
}
...would be more like this in Swift 2.0:
...在 Swift 2.0 中会更像这样:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
var orientations = UIInterfaceOrientationMask.Portrait
if FeatureManager.hasLandscape() {
orientations.insert( UIInterfaceOrientationMask.Landscape )
}
return orientations
}