52

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

For example 65 would returns "A".

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
YourMJK
  • 1,429
  • 1
  • 11
  • 22
  • 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

108

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
  • 529,903
  • 94
  • 1,240
  • 1,382