Swift – Lexical Structure
In this section, we will see the variable valid tokens that forms the basic structure of the swift program also called the lexical structure. The valid tokens include,
Comments are those statements intended to add some additional information to fellow developers. These statements are ignored and skipped by the compiler. We have two types of comments namely single line comments and multiline (or block) comments as shown below in example.
// Single line comment /* Multi line (or block) comment - can use multiple lines */
Whitespace is used to separate different tokens and also add readability to code. Whitespace characters for readability especially used in binary operations are ignored and skipped by the compiler. An example shown below in example.
var a = 19 var sum = a + 20
You can find whitespace characters found in the above example. The space between var and a are mandatory as they are two different tokens. But the space between a and =, and the space between = and 19 are just for readability. Similar whitespace characters are used in sum = a + 20 only for readability.
Identifiers can contain alphabets, numbers and combining unicode characters. Identifiers can begin with an upper case or lower case letter A through Z, an underscore (_), a noncombining alphanumeric unicode character in the basic multilingual plane, or a character outside the basic multilingual plane that isn’t in a private use area as per apple swift language guide. Due to support of unicode characters, its now possible to name variables in your native languages across the world.
As in all programming languages, you cannot use keywords for identifiers. But, if you still want to use it, keyword can be used by adding a backtick(`) before and after it. But x and `x` are not different. Examples of valid identifiers are shown in following example.
_variable `class` name number123 变量 भेरिएबल متغير variável পরিবর্তনশীল переменная 変数
The list of keywords in swift are listed below.
Literals can be any three of the following.
- Integer literals
- Floating point literals
- String literals
These are nothing but values we assign to variables. Examples of the above literals are given below.
// Integer literal 10 var a = 10 // Floating point literal 10.4 var b = 10.4 var c = "Hello"
Integer literal can be represented in decimal, binary, octal and hexadecimal forms. An example for representing a integer decimal number 20 in different forms is shown below.
//Decimal integer literal 20 var a = 10 //Binary integer literal 20 var b = 00010100b //Hexadecimal integer literal 20 var c = 14x //Octal integer literal 20 var d = 24o
One of the new features available in swift is that you can use underscore(_) between digits for readability as shown below. Similarly, leading zeros will be ignored by compiler.
var a = 100_000_000
Floating point Literals
Floating point numbers can be simple decimal numbers, exponential numbers both decimal and hexadecimal floating point numbers. An simple example is shown below.
//Simple floating point number var a = 10.5 //Exponent floating point number for 1050 var b = 10.5e2 //Exponent floating point number for 0.1050 var c = 10.5e-2 //Hexa decimal exponent floating point number for 40 var d = 0xAp2 //Hexa decimal exponent floating point number for 2.5 var d = 0xAp-2
String literals are characters are enclosed within double quotes. Strings can contain escape sequences to represent characters like qoutes. Example for string literal is shown below.
var a = "test" var a = "Hello\nWorld"
There are different operators supported in swift which includes + ,- ,* , /, %, ^, &, &&,| , ||, ++, –, ~, < ,> ,. ,-> ,!. The list of operators are explained in swift- operators tutorial.