Switch statement for imported NS_OPTIONS (RawOptionSetType) in Swift?(Swift中导入的NS_OPTIONS(RawOptionSetType)的切换语句?)
问题描述
Swift 中的 switch 语句更具表现力.我想知道这是否可能:
The switch statement in Swift is so much more expressive. I'm wondering if this might be possible:
让我们以 UIViewAutoresizing 为例.在 Objective-C 中定义如下:
Lets look at UIViewAutoresizing for example. It's defined in Objective-C as follows:
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
我可以像枚举一样在 Swift 中使用它:
I can use it in Swift like an enum:
let foo = UIViewAutoresizing([.FlexibleHeight, .FlexibleTopMargin])
是否可以使用 switch 语句代替多个 if 语句?
Is it possible to use a switch statement instead of multiple if-statements?
if foo & UIViewAutoresizing.FlexibleHeight != nil {
}
if foo & UIViewAutoresizing.FlexibleWidth != nil {
}
if foo & UIViewAutoresizing.FlexibleTopMargin != nil {
}
类似这样的伪代码:
switch foo { // ** THIS IS PSEUDO CODE AND WILL NOT COMPILE **
case & .FlexibleHeight:
println("height")
case & .FlexibleWidth:
println("width")
case & .FlexibleTop:
println("top")
}
推荐答案
我对这个问题感到非常沮丧,所以我编写了一个 Bitmask
I was frustrated enough about this problem that I wrote a Bitmask<T>
class that can handle these use cases. The code is up on Github: brynbellomy/SwiftBitmask
它允许你用任何类型的对象作为你的底层类型(这里我使用 enum
):
It allows you to do stuff like this with any kind of object as your underlying type (here I'm using an enum
):
enum MonsterAttributes : IBitmaskRepresentable, IAutoBitmaskable {
case Big, Ugly, Scary
static var autoBitmaskValues : [MonsterAttributes] = [.Big, .Ugly, .Scary,]
var bitmaskValue: UInt16 { return AutoBitmask..autoBitmaskValueFor(self) }
init(bitmaskValue: UInt16) { self = AutoBitmask.autoValueFromBitmask(bitmaskValue) }
}
// various ways to initialize
let option : MonsterAttributes = .Ugly
let bitmaskOfOption = Bitmask(option)
let anotherBitmaskOfOption = |MonsterAttributes.Ugly // same as bitmaskOfOption
let orWithVar = option | .Big // == Bitmask<MonsterAttributes> with a bitmaskValue of 1 | 2
let simpleOr = MonsterAttributes.Big | .Ugly // == Bitmask<MonsterAttributes> with a bitmaskValue of 1 | 2
// getting the raw integral bitmask value
let simpleOrValue = simpleOr.bitmaskValue // == UInt16(1 | 2)
let orValue = (MonsterAttributes.Big | .Ugly).bitmaskValue // == UInt16(1 | 2)
// implements BooleanType
if simpleOr & .Ugly { /* this code will execute */ }
// supports pattern matching operator
if simpleOr ~= .Ugly { /* this code will execute */ }
if simpleOr ~= (.Ugly | .Scary) { /* this code will execute */ }
...您所要做的就是实现一个单一属性协议.
... and all you have to do is implement a one-property protocol.
我真的很好奇是否有人对代码有任何反馈或想法,所以如果您有任何想法,请在队列中留下问题!
I'm really curious if anyone has any feedback on or ideas for the code, so please leave an issue in the queue if you think of anything!
这篇关于Swift中导入的NS_OPTIONS(RawOptionSetType)的切换语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift中导入的NS_OPTIONS(RawOptionSetType)的切换语句?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01