Enum of tuples in Swift

Swift enumerations cannot have Tuples as a raw value type.

Alternative approaches include storing the code and deriving a description from that:

enum ErrorCode: Int, CustomStringConvertible {
    case Generic = 0
    case DB = 909

    var description: String {
        switch self {
        case .Generic:
            return "Unknown"
        case .DB:
            return "Database"
        }
    }
}

…or storing associated values for code and description in the enumeration cases themselves:

enum Error {
    case Generic(Int, String)
    case DB(Int, String)
}

If you’re just looking for constant values, @matt’s suggestion of organizing them within a struct would work, too.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)