Swift is a programming language created by Apple in 2014. It quickly bypassed Objective C as the preferred choice of app developers. It is open source and offers many advantages over Objective C.

Its latest release brings ABI stability across Apple’s platforms, backward compatibility with older versions, and renewed documentation. It also features new struct types and categories, which allow extending classes at runtime without needing to subclass them.

Syntax

Swift uses a simple and clean syntax to make code easier to read and maintain. It also features safety precautions that reduce the chance of mistakes and improve readability. It borrows some features from Objective-C, such as named parameters, while introducing advanced types such as tuples. These advanced types allow you to pass around groups of values as a single compound value.

Variables and constants in Swift have a special attribute called type that specifies the kind of value they hold. The compiler can usually infer the type of a variable or constant when you set it. For example, if you name a variable welcomeMessage, the compiler can infer that it is an integer.

Like other programming languages, Swift requires you to use lowercase letters (A to Z), underscores _, and digits (0 to 9). You can also include punctuation such as periods and question marks. You should not use reserved words, however. Swift does not require you to use a semicolon at the end of every line of code, but you can add them for clarity.

Types

Like most programming languages, Swift provides a full complement of built-in data types that store numbers, characters, strings, and boolean values. These include String, Int, Float, and Double. Int is a 32-bit integer that can hold numeric literal values and constants. Float is a floating-point number that stores up to six decimal places, while Double is a 64-bit number that can store more precision.

When you declare a variable, Swift will check what kind of value it will contain and ensure that the code will work with that type. This is known as type safety. For example, if you try to pass a variable with a different type to a function, Swift will raise an error at compile time.

Most Swift operators support value types, including comparison and logical operations. In addition, functions can declare themselves as throwing or not-throwing. This means that any errors that occur within a function will be propagated out of the scope until caught by one or more catch clauses. This is a powerful feature that allows Swift to be more resilient.

Variables

Unlike in C, Swift variables can have any data type and do not require initialization at declaration time. This allows the compiler to determine their types at build time using type inference. The compiler can also use this information to optimize their memory usage, e.g. by converting a variable of type Int to a UInt if possible.

Variables can be named with any identifier that is valid in the scope in which they are declared, including Unicode characters, emojis, and private-use Unicode scalar values. However, they cannot start with a number, and cannot contain whitespaces, mathematical symbols, or arrows. They must also be unique within the scope.

Swift supports advanced types such as tuples, which combine multiple values into a single compound value. This makes it easier to pass around groups of values in functions. It also makes code safer and clearer in intent when working with values that don’t need to change, such as a constant. It can also be used to make conditional statements such as while, if, and switch more powerful, such as by supporting pattern matching and conditionally unwrapping optionals.

Operators

Swift has the same basic operators found in other languages, including arithmetic (like + and -), comparison, and logical. It also has a new identity operator that checks whether two object references refer to the same object instance, and control statements like while, for, if and switch that support pattern matching and conditionally unwrapping optionals.

Like in other programming languages, Swift uses a set of rules that determines the precedence of operators in expressions. This means that operators with higher precedence are executed before those with lower precedence. For example, multiplication has more precedence than addition, so 3 * 4 is calculated before 2 + 4.

In addition to the standard arithmetic and comparison operators, Swift supports a number of compound assignment operators, which combine an operation with assigning a value to a variable. Table 3.1 shows each of the operators, what code they are short for, and their description.

Control flow

The Swift language includes a variety of control flow structures. These include loop statements, if-else statements, and switch statements. These statements allow you to perform different actions based on specific conditions. Swift also features an efficient memory management system with tight, deterministic reference counting. This means that you can use the language to create applications with minimal memory usage.

Swift also supports attributes, which are metadata that are read by the development environment but are not necessarily part of the compiled code. These can be used to modify the behavior of a class, for example, by defining a closure or specifying a weak or unowned pointer.

In addition, Swift offers a number of control flow structures, such as for-in loops and a range of data types. Its Countable Range data type, for example, lets you define sequences of countable integers. Countable ranges can be open or half-open, and the second value in a range must be greater than or equal to the first. This is an important feature because it allows you to build more complex data-driven logic.