enum Personel: String, CaseIterable {
case teddy = "Teddy"
case kitty = "Kitty"
case bonny = "Bonny"
case smokey = "Smokey"
}
Is there anyway I can avoid having to hand assign capitalized rawValue? Just do this:
enum Personel: String, CaseIterable {
case teddy
case kitty
case bonny
case smokey
}
Of course I can just capitalize the case value itself. But seems it's Swift style to use lower case. Can I have both follow good code style and get capitalized rawValue?
would be nice if I can override rawValue getter and insert my code...
With RawRepresentable, I still have to manually spell out the capitalized form of each case rawValue...with even more code than simply hand assigning the String rawValue of each case.
For now, I just add a getter func to the enum to return the capitalized rawValue.
from experience, you almost never want to do this. You don't want to tie the prosaic representation to the Swift identifier or else you're eventually going to end up with
enum
{
case fooBar // -> 'FooBar', not 'Foo Bar'
case _2D // -> '_2D', not '2D'
case oreilly // -> 'Oreilly', not 'O'Reilly'
}
a computed String property with a switch over self is verbose, but it's almost always what you're going to end up with anyway.