Search code examples
swiftconditional-compilation

Conditionally-compiled case statement in swift


I've implemented an enum statement in Swift with computed properties. I want to add values that are conditionally compiled, e.g. only for certain configurations.

Here is an example of what I'm trying to do:

enum Rows: Int
{
    case Row1
    case Row2
    #if DEBUG
    case Debug
    #endif

    var rowTitle: String
    {
        case Row1: return "Row 1"
        case Row2: return "Row 2"
        #if DEBUG
        case Debug: return "Debug"
        #endif
    }
}

This does't seem to be supported as I get the following error message on the case statement within the switch statement:

'case' label can only appear inside a 'switch' statement

Is there a way to do this in Swift?

Thanks,
David


Solution

  • Conditional compilation of enum cases now works as of Swift 4.

    enum Foo {
        case bar
    
        #if DEBUG
        case baz
        #endif
    }
    
    let bar = Foo.bar
    
    #if DEBUG
    let baz = Foo.baz
    #endif