Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift typealias for enum case

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 cases 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!

like image 397
Clay Ellis Avatar asked Aug 22 '15 22:08

Clay Ellis


1 Answers

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
like image 161
jtbandes Avatar answered Oct 19 '22 20:10

jtbandes