combine enum's the RawValue type define and it's protocol as the one
ZhipingYang opened this issue · 1 comments
ZhipingYang commented
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 RawRepresentableXXX
method already be achieved (no more code as the blow)
ZhipingYang commented
older test case
struct AccessibilityID {
enum Home: String {
case setting = "HomeSetting"
}
enum Welcome: String, PrettyRawRepresentable {
case setting1
case setting2 = "WelcomeSetting"
}
}
let h_path = AccessibilityID.Home.setting.rawValue // "HomeSetting"
let w_path1 = AccessibilityID.Welcome.setting1.rawValue // "setting1"
let w_path1 = AccessibilityID.Welcome.setting1.prettyRawValue // "AccessibilityID_Welcome_setting1"
let w_path1 = AccessibilityID.Welcome.setting2.prettyRawValue // "WelcomeSetting"
new test case
struct AccessibilityID {
enum Home: String {
case setting = "HomeSetting"
}
enum Welcome: XXX {
case setting1
case setting2 = "WelcomeSetting"
}
}
let path1 = AccessibilityID.Home.setting.rawValue // "HomeSetting"
let path2 = AccessibilityID.Welcome.setting1.rawValue // "AccessibilityID_Welcome_setting1"
let path3 = AccessibilityID.Welcome.setting2.rawValue // "WelcomeSetting"