Created
May 11, 2015 13:12
-
-
Save gscalzo/1c3f53bc9e3ba5e0ae30 to your computer and use it in GitHub Desktop.
From String To Token
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
import Foundation | |
enum LayoutToken: StringLiteralConvertible { | |
case Plain(Int) | |
case Container(Int) | |
case ComponentId(Int) | |
func description() -> String{ | |
switch self { | |
case .Plain(let value): | |
return "\(value)" | |
case .Container(let value): | |
return "container+\(value)" | |
case .ComponentId(let value): | |
return "componentId+\(value)" | |
} | |
} | |
init(stringLiteral value: String) { | |
self = buildFromString(value) | |
} | |
init(extendedGraphemeClusterLiteral value: String) { | |
self = buildFromString(value) | |
} | |
init(unicodeScalarLiteral value: String) { | |
self = buildFromString(value) | |
} | |
} | |
private func buildFromString(value: String) -> LayoutToken{ | |
if (value.rangeOfString("container") != nil) { | |
return .Container(15) | |
} else if (value.rangeOfString("componentId") != nil) { | |
return .ComponentId(15) | |
} else { | |
return .Plain(30) | |
} | |
} | |
//let string: LayoutToken = "container+20" | |
let string: LayoutToken = "componentId+20" | |
switch string { | |
case LayoutToken.Plain: | |
println("Plain") | |
case LayoutToken.Container: | |
println("Container") | |
case LayoutToken.ComponentId: | |
println("ComponentId") | |
} | |
//let plain = LayoutToke |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment