Hoping you can help - this may be a simple question.
I've got a list of the unicode values of emojis, and I'm trying to randomly display one. Here is where I'm hardcoding displaying the smiley:
VStack {
Text(String("\u{1F600}")).font(.largeTitle).foregroundStyle(Color.white).bold().padding(25)
}
I want to be able to pull the value from a string variable, but I'm getting a bit stuck on trying to escape for unicode AND escape for string interpolation at the same time.
I've tried this:
Text(String("\u{\(gameModel.thisWord)}")).font(.largeTitle)
.foregroundStyle(Color.white).bold().padding(25)
...but I get hit with "Expected } in \u{...} escape sequence."
I've also tried going working around it with this sort of thing:
var updatedString = "\u{99999}".replacingOccurrences(of: "99999", with: gameModel.thisWord)
Text(String(updatedString)).font(.largeTitle)
.foregroundStyle(Color.white).bold().padding(25)
...but that doesn't translate the unicode into the emoji.
Any help would be greatly appreciated!
Thanks!
Mark
One cannot interpolate the unicode escape sequence in a Swift string as the string does not contain the text of the escape sequence — it is ‘compiled’ into the character much earlier hence the error message expecting the rest of the escape sequence.
If the type of gameModel.thisWord
is String, you can use Int with radix 16 to parse it as hex, then use UnicodeScalar to produce the character, which String can produce a string from.
String(UnicodeScalar(Int(gameModel.thisWord, radix: 16)!)!)