MatterGoal MatterGoal - 5 months ago 5
Swift Question

Enum of tuples in Swift

Is this possible to create a enum of Tuples in Swift?

I'd like to build something like:

enum ErrorCode: (Int, String) {
case Generic_Error = (0, "Unknown")
case DB_Error = (909, "Database")
}


But it doesn't compile... Is there a way to obtain a similar result?

Answer

It depends what you mean by "similar". What I do is use a Struct with static constant properties:

struct Trouble {
    static let Generic_Error = (0, "Unknown")
    static let DB_Error = (909, "Database")
}

Now things like Trouble.Generic_Error are usable throughout your code.