Character and String Data Types
Explore Swift's character and string data types to understand how single characters and text sequences are stored and manipulated. Learn to use string interpolation and multi-line string literals, and handle special characters with escape sequences. This lesson equips you to work effectively with text data in Swift programming.
Character data type
The Swift Character data type is used to store a single character of rendered text such as a letter, numerical digit, punctuation mark, or symbol. Internally, characters in Swift are stored in the form of grapheme clusters. A grapheme cluster is made of two or more Unicode scalars that are combined to represent a single visible character.
The following lines assign a variety of different characters to Character type variables:
Note: If a single character is assigned to a constant or variable using type inference, the compiler will infer that the value is of type
Stringinstead ofCharacter. If you specifically need the constant or variable to be of typeCharacteryou must declare it as such using type annotation.
Characters may also be referenced using Unicode code points. The following example assigns a star symbol to a variable using Unicode, and then prints it:
String data type
The String data type is a sequence of characters that typically make up a word or sentence. In addition to providing a storage mechanism, the String ...