How is πŸ‡©πŸ‡ͺcharacter represented in Swift lines?

Like some other emoji characters, the combination 0x0001F1E9 0x0001F1EA (the German flag) appears as a single character on the screen, although in reality these are two different combinations of Unicode characters. Is it represented as one or two different characters in Swift?

+6
source share
4 answers
let flag = "\u{1f1e9}\u{1f1ea}" 

then flag is πŸ‡©πŸ‡ͺ.

For additional regional indicator symbols, see http://en.wikipedia.org/wiki/Regional_Indicator_Symbol

+17
source

Support for "extended grapheme clusters" has been added to Swift in the meantime. Iterating over string characters results in a single character for "flags":

 let string = "HiπŸ‡©πŸ‡ͺ!" for char in string.characters { print(char) } 

Output:

  H
 i
 πŸ‡©πŸ‡ͺ
 !
+4
source

Swift does not tell you what the internal representation of String . You interact with String as a list of full-size (32-bit) Unicode code points:

 for character in "Dog!🐢" { println(character) } // prints D, o, g, !, 🐢 

If you want to work with a string as a sequence of UTF-8 or UTF-16 code points, use its utf8 or utf16 . See Strings and characters in documents.

+2
source

Swift 3 implements Unicode in its String structure. In Unicode, all flags are pairs of Regional Indicator Symbols . So, πŸ‡©πŸ‡ͺ actually πŸ‡© followed by πŸ‡ͺ (try copying two and pasting them next to each other!).

When two or more regional indicator symbols are placed next to each other, they form an "expanded grapheme cluster", which means that they are considered as one symbol. That's why "πŸ‡ͺπŸ‡Ί = πŸ‡«πŸ‡·πŸ‡ͺπŸ‡ΈπŸ‡©πŸ‡ͺ...".characters gives you ["πŸ‡ͺπŸ‡Ί", " ", "=", " ", "πŸ‡«πŸ‡·πŸ‡ͺπŸ‡ΈπŸ‡©πŸ‡ͺ", ".", ".", "."] .

If you want to see every single Unicode code point (AKA "scalar"), you can use .unicodeScalars , so "HiπŸ‡©πŸ‡ͺ!".unicodeScalars gives you ["H", "i", "πŸ‡©", "πŸ‡ͺ", "!"]

TL; DR

πŸ‡©πŸ‡ͺ is one character (both in Swift and Unicode), which consists of two code points (AKA scanners). Do not forget that these are different! πŸ™‚


see also

+2
source

Source: https://habr.com/ru/post/970210/


All Articles