Created
August 15, 2014 17:27
-
-
Save jarsen/904bcda1da76528b0d72 to your computer and use it in GitHub Desktop.
Making an enum out of your own classes. Not sure if this is a great idea, but it works and is cool.
This file contains hidden or 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
class Name : StringLiteralConvertible, Equatable { | |
var firstName: String | |
var lastName: String | |
required init(firstName: String, lastName: String) { | |
self.firstName = firstName | |
self.lastName = lastName | |
} | |
class func convertFromStringLiteral(value: StringLiteralType) -> Self { | |
let names = value.componentsSeparatedByString(" ") | |
var firstName = names[0] | |
var lastName = names[1] | |
return self(firstName: firstName, lastName: lastName) | |
} | |
class func convertFromExtendedGraphemeClusterLiteral(value: String) -> Self { | |
let names = value.componentsSeparatedByString(" ") | |
var firstName = names[0] | |
var lastName = names[1] | |
return self(firstName: firstName, lastName: lastName) | |
} | |
} | |
func ==(lhs: Name, rhs: Name) -> Bool { | |
return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName | |
} | |
enum DefaultNames: Name { | |
case Me = "Jason Larsen" | |
case You = "John Smith" | |
} | |
DefaultNames.Me | |
DefaultNames.You.toRaw() | |
if let name1 = DefaultNames.fromRaw("Jason Larsen") { | |
switch(name1) { | |
case .Me: | |
println("This is me") | |
case .You: | |
println("This is you") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment