0

I am trying to accomplish the following:

I have an enum

enum MyEnum {
  case Position(x: Int, y: Int)
  case Name(String)
}

and I want to be able to do the following:

MyEnum.caseCount // returns 2
let val = MyEnum.Position(x: 0, y: 0)
val.idx // returns 0
MyEnum.Name("My awesome name").idx // returns 1

So I want a variable holding the total amount of cases + on a specific enum case I want to know its index in the enum

Is there a way I can automatically derive this (probably will be using a protocol)?

I have looked at CaseIterable, but that can't automatically be implemented for enums holding values. I also can't do = enum MyEnum: Int because of the value again.

Another SO question had a lot of answers, all implented in Swift 3, and none of those seemed to still work.

If unsafe code would have to be used for this, I would be ok with that.

Something like this would work ofcourse, however it is a lot of boilerplate for the user to write:

extension MyEnum {
  func id() -> Int {
    switch self {
      case .Position: return 0
      case .Name: return 1
    }
  }
}
Jomy
  • 514
  • 6
  • 22
  • do you have any snippets of what you tried? – JCode Jun 03 '22 at 15:08
  • @JCode I have linked the SO question (https://stackoverflow.com/questions/27094878/how-do-i-get-the-count-of-a-swift-enum). I've tried all solutions that didn't mention anything about enums with raw values. But none of these answers actually worked. – Jomy Jun 03 '22 at 15:13
  • You have an enum with associated values which means the number of cases is unknown, `.Name("a")` and `.Name("b")` are two different cases for instance. You could supply that yourself but given the type of associated values you have I suspect the number of cases are unlimited? – Joakim Danielson Jun 03 '22 at 15:15
  • @JoakimDanielson case `.Name("a")` and `.Name("b")` should be treated as the same index in this example – Jomy Jun 03 '22 at 15:18
  • I have never done this in Swift but you can use a Mirror to get the names of the cases by reflection, right? That should let you determine the case count and find the index of a given case. Something along the lines of [Reflectable enums in Swift 3](https://ijoshsmith.com/2017/04/08/reflectable-enums-in-swift-3/) or [Dynamic access of Swift Enum associated values](https://medium.com/@nguyenminhphuc/reflection-of-enum-in-swift-ed5a31573f28). – David Conrad Jun 03 '22 at 20:17
  • @DavidConrad I've had a quick look at those links, and it looks like that's what I'm after. Haven't had time to verify if this works for my use case yet, but should be able to in the next couple of days – Jomy Jun 04 '22 at 20:10
  • @DavidConrad Seems like I'm unable to get the cases when using `Mirror(reflectin: MyEnum.self)` – Jomy Jun 04 '22 at 21:50
  • I'm sorry, I'm out of my depth here. This is more complex than anything I have done with Swift. I just thought of how I might do it with Java reflection, and then Googled to see if there was something similar in Swift. I've only used Swift for pretty simple, straightforward things. Good luck, though! – David Conrad Jun 06 '22 at 19:03
  • @DavidConrad No worries! It was a good suggestion, just a bummer it’s not as powerful. I came up with a sub-optimal hacks solution, but it’s way slower than doing this manually, so currently it’s still manual. – Jomy Jun 08 '22 at 00:18

0 Answers0