Skip to content

Instantly share code, notes, and snippets.

@danhd123
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danhd123/c1dc8242f48ace176cf8 to your computer and use it in GitHub Desktop.
Save danhd123/c1dc8242f48ace176cf8 to your computer and use it in GitHub Desktop.
How to make Strings work in a Uint8 raw value enum.
extension UInt8 : StringLiteralConvertible {
typealias ExtendedGraphemeClusterLiteralType = String
typealias UnicodeScalarLiteralType = String
public init(stringLiteral value: StringLiteralType){
self.init(([UInt8]() + value.utf8)[0])
}
public init(extendedGraphemeClusterLiteral value: String){
self.init(([UInt8]() + value.utf8)[0])
}
public init(unicodeScalarLiteral value: String){
self.init(([UInt8]() + value.utf8)[0])
}
}
enum Tag : UInt8 {
case Byte = "B" // this works now!
case Char = "C"
case Double = "D"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment