terr terr - 4 months ago 9
Swift Question

Enum case shadowing a built-in type name

I have created an enum

enum CellType {
case String
case TextView
case Date
case Int
case Float
case Radiobox(data: [String])
case Checkbox
case Email
case Boolean
case Image
case Empty
}


Because I have a
case String
I can't use
String
in
case Radiobox(data: [String])
, instead I get an error. But if I remove the
case String
, there's no error.

Is there a way to get around this?

Answer

All build-in types are under module Swift so you can refer to the build-in String type as Swift.String

enum CellType {
    case String
    case TextView
    case Date
    case Int
    case Float
    case Radiobox(data: [Swift.String])
    case Checkbox
    case Email
    case Boolean
    case Image
    case Empty
}

let a = CellType.String
let b = CellType.Radiobox(data: ["s"])