Getting the name of a Swift enum value

Use of unresolved identifier 'reflect'

Where is reflect from?

Any moderately large enum switch compiles down, usually, to a jump table. This is not terribly expensive, but it does defeat the branch target predictor as the jump is highly unpredicable. It also incurs a code size cost to compile that jump table, and the cost of constructing the raw value.

I was under the same impression initially but CustomStringConvertible gets refined by LosslessStringConvertible, which does have semantics in its description (as in it requires T(t.description) == t)

1 Like

Wow that's some black magic I knew nothing about. You wouldn't happen to have a similar incantation to get a String (property name) from a KeyPath? Eg:

struct Foo {
    var color: Color
}
let kp = \Foo.color
???(kp) == "color" 

Rob

If it’s an @objc property, then I think you can access it via ._kvcKeyPathString (but keep in mind this is an undocumented API).

There’s an open bug to add an API to get the string representation of a (Swift) key path: [SR-5220] Expose API to retrieve string representation of KeyPath · Issue #4085 · apple/swift-corelibs-foundation · GitHub

@suyashsrijan

Using your code with @_silgen_name("swift_EnumCaseName") works wonders. However, will it cause me to fail Apple's app review for an iOS app?

E.g. is this considered a private API.

As far as I am aware, compiler internal attributes are not the same as using private APIs from Apple SDKs and so it won’t result in rejection.