Alright, I've done my homework and read what I could find but I can't seem to find if this is possible to accomplish with Swift. I've got an enum that I use all over the place: SomeEnum
and one if it's case
s is a lengthy SomeEnum.SomeLengthyCaseName
and I'm tired of seeing it all over my code. I don't want to refactor because I like the descriptive nature of the case for when newcomer's maintain my code.
So here's the question: Is it possible to create a typealias
for SomeEnum.SomeLengthyCaseName
? And if so, how? Here's what I've tried:
enum SomeEnum {
case SomeLengthyCaseName
}
typealias SLCN = SomeEnum.SomeLengthyCaseName
That's the syntax but Xcode gives a compiler error saying that "SomeLenghtyCaseName
is not a member of SomeEnum
."
Ready, set, go!
That's a misleading error message.
The real problem is that SomeLengthyCaseName
is not a type. Therefore you can't use typealias
, which is only for aliases of types. (For example, you could say typealias SE = SomeEnum
.)
Instead, you can just use a global constant:
let SLCN = SomeEnum.SomeLengthyCaseName
Or, better, a static constant on the enum itself:
enum SomeEnum {
case SomeLengthyCaseName
static let SLCN = SomeEnum.SomeLengthyCaseName
}
let x: SomeEnum = .SLCN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With