Created
September 29, 2014 02:46
-
-
Save natecook1000/1c9fb74c42808bcd0b69 to your computer and use it in GitHub Desktop.
toUnicodeScalar() method for Character
This file contains 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
extension Character { | |
func toUnicodeScalar() -> UnicodeScalar? { | |
let s = String(self) | |
if distance(s.unicodeScalars.startIndex, s.unicodeScalars.endIndex) > 1 { | |
return nil | |
} | |
return s.unicodeScalars[s.unicodeScalars.startIndex] | |
} | |
} | |
let char: Character = "A" | |
let dog: Character = "🐶" | |
let flag: Character = "🇺🇸" | |
println(char.toUnicodeScalar()?.value) | |
// Optional(65) | |
println(dog.toUnicodeScalar()?.value) | |
// Optional(128054) | |
println(flag.toUnicodeScalar()?.value) | |
// nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment