Enum Foo: String { case bar = "blah" } print(Foo.bar) // <== "bar", how "bar" gets printed?

enum Foo: String {
    case bar = "blah"
}

print(Foo.bar)  // "bar"   <= what this? How does "bar" get printed?

by what mechanism is "bar" gets printed?

1 Like

Because the value of the case is bar. If you want to print out the string value of the case use bar.rawValue.

2 Likes

I was wondering how did print get the string value of "bar"? As @Peter-Schorn point out, it's using this internal func _adHocPrint_unlocke....

If we want to get the case name "bar" string out of an enum case value, we will have to go through reflection. There is no built-in simple way.

Remember: Swift is open source. Just read the source code.

Terms of Service

Privacy Policy

Cookie Policy