Equivalence of structs? is typealias the "Swift" way to do this?

62 views Asked by At

Maybe I'm over thinking this or having a brain cramp.

typealias myZero = reallyLongAndObfuscatedEnum.ZERO

Let's say I have some legacy code and need to convert it to Swift 3. This is of course a very simplified version of what I'm dealing with.

enum reallyLongAndObfuscatedEnum {

  ///zero
  struct ZERO {
    static let float_t:Float32   = Float32(0)
    static let double_t:Float64  = Float64(0)
    static let cgfloat_t:CGFloat   = CGFloat(0)
    static let desc:String     = "/* nothingness, nada, it is the void  */"
  }
}

Now instead of typing:

 ReallyLongEquation * reallyLongAndObfuscatedEnum.ZERO.cgfloat_t  

I can use:

ReallyLongEquation * myZero.cgfloat_t

Is typealias the "Swift" way to do this?

1

There are 1 answers

0
AudioBubble On BEST ANSWER

If all you are looking for is an alternative (and shorter) name for reallyLongAndObfuscatedEnum.ZERO, then yes typealias would be the way to go.

At the risk of leading you down the wrong path (since I don't have the full context), you may also want to look at Associated Values in Swift enums. For instance you could rewrite the enum as

enum Example {
    case Zero(float_t: 0.0f, double_t: 0.0, cgfloat_t: CGFloat(0))

    var description: String {
        switch self {
        case .Zero:
            return "The Emptiness of Void"
        }
    }
}