Hello,
I am trying to find a convenient and reusable way to be able to create a string based on an enum's case.
Here is a very simple example:
enum MyEnum: Int {
case one
case two
// imagine a lot of cases...
}
struct SomeStruct {
let myEnum: MyEnum
}
let test = SomeStruct(myEnum: .one)
print(test)
When printing this (in a playground) i get this result in the console
SomeStruct(myEnum: __lldb_expr_17.MyEnum.one)
instead of __lldb_expr_17.MyEnum.one
I would like to print just ".one"
The example above is quite simple however this is not only limited to playgrounds.
The main reason I want to have such a simple print is for better console / file logging and unit test assert messages.
If say MyEnum was part of another library, it will always have the Type.case
format and in some cases have Module.Type.case
format. This verbose description makes reading logs and unit tests much harder than a short name like ".one".
I am working on a quite large code base with many such enums that contain a lot of cases. Writing manual debugDescription
for all of them would be cumbersome and since it is based on strings it can be easy to write incorrect implementation especially after refactoring some cases without the compiler warning me.
I tried experimenting and made a simple function using String(reflecting:)
func enumCaseName<T>(for `case`: T) -> String {
if let caseName = String(reflecting: `case`).split(separator: ".").last {
return ".\(String(caseName))"
} else {
return String(reflecting: `case`)
}
}
extension MyEnum: CustomDebugStringConvertible {
var debugDescription: String {
enumCaseName(for: self)
}
}
When trying to call this within debugDescription
implementation I was surprised to find out that it produced an infinite call stack loop and overflowed.
After reading the documentation for String(reflecting:)
it was obvious why I had the problem, because if the parameter that was passed conformed to CustomDebugStringConvertible
than this initializer would use its debugDescription
instead, thus creating the problem described above.
I would like to hear any suggestions, how to solve this without manually implementing debugDescription
. Additionally many of the enums are of type Int
, so I can not make them of type String
and use their raw value unfortunately. This does not include enums with associated values.
Thank you,
Filip