Einstein
Einstein copied to clipboard
combine enum's the RawValue type define and it's protocol as the one
public protocol PrettyRawRepresentable: RawRepresentable where RawValue == String {
var prettyRawValue: RawValue { get }
}
public extension PrettyRawRepresentable {
var prettyRawValue: String {
let paths = String(reflecting: self).split(separator: ".").dropFirst()
if String(paths.last ?? "") != rawValue {
return rawValue
}
return paths.joined(separator: "_")
}
}
As we already defined
PrettyRawRepresentable's RawValue == String
, but enum cannot write as blow, why?enum Home: PrettyRawRepresentable { case setting } let str = Home.seeting.prettyRawValue // Home_setting
current
enum Home: String, PrettyRawRepresentable {
case setting
}
let str = Home.seeting.prettyRawValue // Home_setting
wants
enum Home: XXX {
case setting
}
let str = Home.seeting.prettyRawValue // Home_setting
-
XXX
can be a protocol or a struct, but should better as a subprotocol of RawRepresentable -
XXX
method already be achieved (no more code as the blow)