44

How can I get a character from a ASCII code in Apple's new Swift?

For example 65 would returns "A".

Paulo Mattos
  • 17,605
  • 10
  • 69
  • 79
YourMJK
  • 1,202
  • 1
  • 9
  • 19
  • If you know the code at compile time you just can use: `println("The Character A: \u{41}")` to print arbitrary unicode scalars. See https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_432 and http://en.wikipedia.org/wiki/Basic_Latin_(Unicode_block) – Klaas Sep 23 '14 at 12:12

1 Answers1

96

As a character:

let c = Character(UnicodeScalar(65))

Or as a string:

let s = String(UnicodeScalar(UInt8(65)))

Or another way as a string:

let s = "\u{41}"

(Note that the \u escape sequence is in hexadecimal, not decimal)

Martin R
  • 510,973
  • 84
  • 1,183
  • 1,314