Skip to content

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

@ZhipingYang

Description

@ZhipingYang
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

Metadata

Metadata

Assignees

Labels

Code Designbetter architecture to discuss

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions