I use the following code to get a Character from a Unicode codepoiint:
let c = Character(Unicode.Scalar("12345")!)It seems very complex and clumsy. Is there an shortcut way to achieve the same goal?
NO.
And the right syntax is
let c = Character(Unicode.Scalar(12345)!)(`Unicode.Scalar("12345")!` causes runtime crash.)
If you often work with `Character` and Unicode codepoint, you can write an extension of your own.
extension Character {
init?(_ codePoint: UInt32) {
guard let us = Unicode.Scalar(codePoint) else {
return nil
}
self = Character(us)
}
}
print(Character(12345)!)Or you can propose a new initializer of `Character` in swift.org .