Hello everyone and welcome to the ninth tutorial in programming with swift brought to you by MadApper. My name is Paul Napier and in this session we will be covering characters.
Characters in programming are another of the basic types. They are a unit of information which have a graphical representation such as a letter, number or punctuation. In Swift you can create a character variable by declaring the type as a Character, then setting the value inside a set of parentheses. i.e.
var char:Character = "a"
Notice, however, that when declaring a character, the value cannot be longer than a single letter.
Another set of characters for you to be aware of are string literals. There are several ones you will come across regularly:
\0 (null character),
\t (horizontal tab),
\n (line feed),
\r (carriage return),
\" (double quote) and
\' (single quote).
You are also able to create string literal characters by declaring their Unicode values. Unicode is an encoding format that is used to represent characters across the world's computers. In order to do this, you can follow the same process as before, but instead of writing the character, you can write a \x followed by a 2 digit hexadecimal number or a \U followed by either a 4 or an 8 digit hexadecimal number representing the character you want.
Although you may not require this technique often, it is still useful to understand that this is how the computer recognises characters and that the capital and lowercase versions of a letter are actually seen as different by the device. So for example the letter A would be \U00000041 where as the lowercase version would be \U00000061.
Another instance where this is useful is for non standard characters, such as glyphs. So a star will be \U00002605 and a hammer and sickle is \U0000262D. Or you may wish to represent alternative alphabets and writing systems such as Arabic, Cyrillic or Chinese.
If you need further examples of characters and their Unicode values there are lots of useful resources on the web. However, one of the best tools is unicode-table.com.
Ok! That's covered off the basics of characters. In the next session we are going to build upon this concept and start talking about strings.