SLN SLN - 2 months ago 6x
Swift Question

Errors when printing Unicode Grapheme Clusters by a for loop

I would like to print out the grapheme clusters of which the code point from U+0021 to U+0100 in the Unicode table

for i in 21...100 {
print("\u{i} ", terminator: "")

The compiler presented me the following error

enter image description here

Question: I guess I can't use the index from the for loop array as the Unicode Scalar indicator in the string interpretation. If so, what steps should I change to use the loop correctly

Many thanks


The \u{n} special character works only with a literal hexadecimal number n. But you can create a unicode scalar from its numerical value:

for i in 0x21...0x100 {
    print(UnicodeScalar(i), terminator: "")