Switch statement for imported NS_OPTIONS (RawOptionSetType) in Swift?

后端 未结 4 1682
既然无缘
既然无缘 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:18

    You definitely can, although it's a little more convoluted now that RawOptionSetType doesn't implement the BooleanType protocol.

    switch foo {
    case let x where x & .FlexibleHeight != nil:
        println("height")
    case let x where x & .FlexibleWidth != nil:
        println("width")
    case let x where x & .FlexibleTopMargin != nil:
        println("top margin")
    default:
        println("no")
    }
    

    Note that this stops on the first condition that matches! So it really is another form of this:

    if foo & .FlexibleHeight != nil {
        println("height")
    } else if foo & .FlexibleWidth != nil {
        println("width")
    } else if foo & .FlexibleTopMargin != nil {
        println("top margin")
    }
    

提交回复
热议问题