Switch statement for imported NS_OPTIONS (RawOptionSetType) in Swift?

后端 未结 4 1651
既然无缘
既然无缘 2021-01-21 12:26

The switch statement in Swift is so much more expressive. I\'m wondering if this might be possible:

Lets look at UIViewAutoresizing for example. It\'s defined in Objecti

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 13:16

    Another solution that I came up with is this:

    let foo = /* .. your value to test .. */
    
    let allCases = [UIViewAutoresizing.FlexibleHeight, UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleTopMargin]
    
    for oneCase in allCases {
        switch oneCase & foo {
    
        case UIViewAutoresizing.FlexibleHeight:
            println("height")
    
        case UIViewAutoresizing.FlexibleWidth:
            println("width")
    
        case UIViewAutoresizing.FlexibleTopMargin:
            println("top")
    
        default:
            break
        }
    }
    

提交回复
热议问题