3

How do you convert unicode emoji stored as a string (e.g. 1f564) to an emoji that could be placed on a UILabel?

let myString = "1f564"

I've seen the use of the escape character but I can't insert variables to replace the characters.

let flag = "\u{1f1e9}\u{1f1ea}"

Thanks

Andrew Varvel
  • 1,286
  • 1
  • 10
  • 19

1 Answers1

13

This works. I don't know if there is anything more direct:

let myStr = "1f564"
let str = String(UnicodeScalar(Int(myStr, radix: 16)!)!)
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • Will this work if the unicode is part of a large string with other normal text? – Nitesh Sep 12 '16 at 12:52
  • @Nitesh, no. In order for this to work you need to extract the hex digits from the larger string, put them into their own string and create the Unicode character from that. – vacawama Sep 12 '16 at 12:57