Last active
April 12, 2016 11:23
-
-
Save marcomasser/b1fddf43badc3c306075d80e85966973 to your computer and use it in GitHub Desktop.
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
enum MyEnum { | |
case IntCase(value: Int) | |
case StringCase(value: String) | |
case IntStringCase(intValue: Int, stringValue: String) | |
} | |
extension MyEnum: RawRepresentable { | |
init?(rawValue: Any) { | |
switch rawValue { | |
case let value as Int: | |
self = .IntCase(value: value) | |
case let value as String: | |
self = .StringCase(value: value) | |
case let value as (Int, String): | |
self = .IntStringCase(intValue: value.0, stringValue: value.1) | |
default: | |
return nil | |
} | |
} | |
var rawValue: Any { | |
switch self { | |
case .IntCase(let value): | |
return value | |
case .StringCase(let value): | |
return value | |
case .IntStringCase(let intValue, let stringValue): | |
return (intValue, stringValue) | |
} | |
} | |
} | |
let foo = MyEnum(rawValue: 42) | |
let bar = MyEnum(rawValue: "Hello") | |
let baz = MyEnum(rawValue: (123, "World")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment