Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection of enum in Swift [duplicate]

Tags:

swift

Say I have an enum like this:

enum Direction {
  case Left
  case Right
}

Is there a way to derive a string description of the enum automatically without defining it myself? Something like this:

let direction: Direction = .Left
let description: String = direction.description // should be "Left"

I tried reflect( direction) and the properties on MirrorType but that didn't work.

like image 250
lassej Avatar asked Dec 03 '25 01:12

lassej


1 Answers

Not currently. You'll need to implement a description function manually for this. Swift has very limited reflection.

like image 163
Rob Napier Avatar answered Dec 04 '25 20:12

Rob Napier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!