Skip to content

Instantly share code, notes, and snippets.

@zaimramlan
Last active August 6, 2019 06:29
Show Gist options
  • Save zaimramlan/8f11b463d1caf150632cb0946c3d025b to your computer and use it in GitHub Desktop.
Save zaimramlan/8f11b463d1caf150632cb0946c3d025b to your computer and use it in GitHub Desktop.
[PLAYGROUND] Associated Enum RawValues
import Foundation
enum Parameter {
enum Module {
case partner(PartnerValues)
enum Keys: String {
case partner
}
enum PartnerValues: String {
case facebook
}
}
}
extension Parameter.Module: RawRepresentable {
typealias RawValue = String
var rawValue: RawValue {
switch self {
case .partner(let value):
return "\(Keys.partner.rawValue)=\(value.rawValue)"
}
}
init?(rawValue: RawValue) {
var val: Parameter.Module?
let parameter = rawValue.components(separatedBy: "=")
if let key = Keys(rawValue: parameter.first ?? "") {
switch key {
case .partner:
if let value = PartnerValues(rawValue: parameter.last ?? "") {
val = .partner(value)
}
}
}
guard let value = val else { return nil }
self = value
}
}
Parameter.Module.partner(.facebook).rawValue
let p = Parameter.Module.init(rawValue: "partner=facebook")!
switch p {
case .partner(.facebook):
print("FACEBOOK YO!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment