Swift中导入的NS_OPTIONS(RawOptionSetType)的切换语句?

Switch statement for imported NS_OPTIONS (RawOptionSetType) in Swift?(Swift中导入的NS_OPTIONS(RawOptionSetType)的切换语句?)

本文介绍了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 类来处理这些用例.代码在 Github 上:brynbellomy/SwiftBitmask

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)的切换语句?

基础教程推荐