5

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

0

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

This is a known bug in Swift. There's no way to currently do conditional compiling inside a switch.

Comments

0

Your syntax for conditional compilation looks correct. However, when I paste your code into a playground, I get multiple errors. The first is at the line beginning with var rowTitle, and the error is "Enums may not contain stored properties".

3 Comments

Sorry, the equal sign shouldn't have been there. The syntax should now be correct.
Yes, so now the issue is that you are using case inside a property definition, not inside a switch statement. You can't do that. The issue is not with the conditional compilation.
I'm confused. How is the case inside a property definition? Is #if DEBUG actually a form of a closure declaration?
0

This is a version of your code that is syntactically correct for the implementation of the computed variable rowTitle:

enum Rows: Int
{
    case Row1
    case Row2

    var rowTitle: String {
            switch self {
            case Row1:
                return "Row 1"
            case Row2:
                return "Row 2"
        }
    }
}

Said that, this is probably what you meant in your question ... but does not work:

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

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

The compiler will not give you any error, but you will not be able to use the Debug case.

1 Comment

The first example you gave is correct if I don't want any conditionally-compiled cases. The second example won't compile because the first switch isn't exhaustive. Also, I want to avoid default cases because I want the compiler to give me an error if I add a new case but forget to handle it in the switch statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.