evie
1
Hi, just a simple question; I'm wondering if it's possible to use a space in a case name because I am using Speech framework. This is my code:
enum Color: String {
case Red, Orange, Yellow
var create: UIColor {
switch self {
case .Red:
return UIColor.red
case .Orange:
return UIColor.orange
case .Yellow:
return UIColor.yellow
}
}
}
If I want someone to have to say "Red Rectangle," instead of just "Red," is this possible? If so, how would I write it in the code?
Thanks in advance.
It's not possible to put a space in a case name (even in backticks). There are a few alternatives, e.g. manually implementing RawRepresentable for your enum.
You can add a specific raw value for the cases with spaces:
enum Color: String {
case red, orange, yellow
case redRectangle = "red rectangle"
}
let red = Colors(rawValue: "red") // .red
let rect = Colors(rawValue: "red rectangle") // redRectangle
Note that I used lowercase case names; it's a convention in Swift to use lowercase for case names.
2 Likes