Skip to content

Instantly share code, notes, and snippets.

@jarsen
Created August 15, 2014 17:27
Show Gist options
  • Save jarsen/904bcda1da76528b0d72 to your computer and use it in GitHub Desktop.
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.
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