Last active
August 6, 2019 06:29
-
-
Save zaimramlan/8f11b463d1caf150632cb0946c3d025b to your computer and use it in GitHub Desktop.
[PLAYGROUND] Associated Enum RawValues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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