0

Here is my enum

enum associatedEnums {
case someIntHere(Int)
case someStrHere(String)

}

var associatedEnumsInst = associatedEnums.someStrHere("String here")

I am able to print using

print(associatedEnumsInst)

I get the output as someStrHere("String here")

If i try to print

print(associatedEnumsInst.rawValue)

I get this error: value of type 'associatedEnums' has no member 'rawValue'

I am expecting a output like this "String here"

I am aware that i can do something like this

switch associatedEnumsInst{
case .someIntHere(let val) : print(val)
case .someStrHere(let val) : print(val)
}

But i wanna get the value without using switch statement and only using rawValue property of enums. Is it possible?

Hamish
  • 78,605
  • 19
  • 187
  • 280
iPhoneDeveloper
  • 958
  • 1
  • 14
  • 23
  • Note the [swift4] tag is only for questions specifically about a *change* in the language for that version, which this is not. The [swift] tag is for general language questions, which this is. I have edited out the [swift4] tag again. – Hamish Oct 21 '17 at 15:30
  • 4
    Enums with associated values have no rawValue. See for example https://stackoverflow.com/questions/24171814/can-associated-values-and-raw-values-coexist-in-swift-enumeration. – Martin R Oct 21 '17 at 15:31
  • 2
    You can add `rawValue: Any` as a computed property but this approach has its own problem: how can you tell the data type and meaning of this `rawValue`? Is it an `Int` or a `String`? What case was it associated with? By the time you add code to figure out these things, the solution is no better (or even worse) than the `switch` statement approach. – Code Different Oct 21 '17 at 15:35

0 Answers0