Swift - Data Type Unicode

Introduction

In Swift, a Character represents a single extended grapheme cluster.

An extended grapheme cluster is a sequence of one or more Unicode scalars that when combined produces a single human-readable character.

Consider the following example:

let hand:Character = "\u{270B}"
let star = "\u{2b50}"
let bouquet = "\u{1F490}"

Here, the three variables are of type Character, with the first one explicitly declared.

Their values are assigned using single Unicode scalars.

Each Unicode scalar is a unique 21-bit number.

Here is another example:

let aGrave = "\u{E0}"

Here, aGrave represents the Latin small letter "a" with a grave.

The same statement can also be rewritten using a pair of scalars-the letter a followed by the COMBINING GRAVE ACCENT scalar:

let aGrave = "\u{61}\u{300}"

Here, the aGrave variable contains one single character.

Consider the following statement:

var voila = "voila"

voila contains five characters.

If you append the COMBINING GRAVE ACCENT scalar to it as follows, the voila variable would still contain five characters:

voila = "voila" + "\u{300}"

Related Topic