Skip to content

Instantly share code, notes, and snippets.

@mattt
Last active August 29, 2015 14:05
Show Gist options
  • Save mattt/1f6c9ab2b4e62cd82a67 to your computer and use it in GitHub Desktop.
Save mattt/1f6c9ab2b4e62cd82a67 to your computer and use it in GitHub Desktop.
Terrible misuse of Swift literal convertibles to automatically create Hiragana transliteration of string value.
struct もじれつ: Printable {
let description: String
init(string: String) {
var mutableString = NSMutableString(string: string) as CFMutableString
if CFStringTransform(mutableString, nil, kCFStringTransformLatinHiragana, 0) == 1 {
self.description = mutableString as NSString
} else {
self.description = string
}
}
}
extension もじれつ: StringLiteralConvertible {
static func convertFromExtendedGraphemeClusterLiteral(value: String) -> もじれつ {
return self(string: value)
}
static func convertFromStringLiteral(value: String) -> もじれつ {
return self(string: value)
}
}
let s: もじれつ = "hiragana" // ひらがな
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment