Einstein icon indicating copy to clipboard operation
Einstein copied to clipboard

combine enum's the RawValue type define and it's protocol as the one

Open ZhipingYang opened this issue 4 years ago • 1 comments

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)

see more: AccessibilityIdentifier

ZhipingYang avatar Aug 01 '19 12:08 ZhipingYang