Swift Literals
Literals refer to values such as specific numbers, strings, or boolean values that clearly indicate their type and assign a value to a variable. For example:
let aNumber = 3 // Integer literal
let aString = "Hello" // String literal
let aBool = true // Boolean literal
Integer Literals
Integer literals can be decimal, binary, octal, or hexadecimal constants. Binary literals are prefixed with 0b
, octal literals with 0o
, hexadecimal literals with 0x
, and decimal literals have no prefix:
Here are some examples of integer literals:
let decimalInteger = 17 // 17 - decimal representation
let binaryInteger = 0b10001 // 17 - binary representation
let octalInteger = 0o21 // 17 - octal representation
let hexadecimalInteger = 0x11 // 17 - hexadecimal representation
Floating-Point Literals
Floating-point literals consist of an integer part, a decimal point, a fractional part, and an exponent part.
Unless specified otherwise, the default inferred type for floating-point literals is Double
, representing a 64-bit floating-point number.
Floating-point literals are typically expressed in decimal (with no prefix) or hexadecimal (with the 0x
prefix).
Decimal floating-point literals are composed of a sequence of decimal digits followed by a decimal fraction or exponent (or both). The decimal fraction consists of a decimal point .
followed by a sequence of decimal digits. The exponent part consists of a prefix (either e
or E
) followed by a sequence of decimal digits, indicating multiplication by 10 to the power of the exponent. For example, 1.25e2
represents 1.25 × 10^2
, which is 125.0
; similarly, 1.25e-2
represents 1.25 × 10^-2
, which is 0.0125
.
Hexadecimal floating-point literals consist of a 0x
prefix followed by an optional hexadecimal fraction and a hexadecimal exponent. The hexadecimal fraction consists of a decimal point .
followed by a sequence of hexadecimal digits. The exponent part consists of a prefix (either p
or P
) followed by a sequence of decimal digits, indicating multiplication by 2 to the power of the exponent. For example, 0xFp2
represents 15 × 2^2
, which is 60
; similarly, 0xFp-2
represents 15 × 2^-2
, which is 3.75
.
Negative floating-point literals are composed of the unary minus operator -
followed by a floating-point literal, such as -42.5
.
Floating-point literals can use underscores _
to enhance readability, which are ignored by the system and do not affect the literal's value. Similarly, leading zeros in numbers do not affect the literal's value.
Here are some examples of floating-point literals:
let decimalDouble = 12.1875 // Decimal floating-point literal
let exponentDouble = 1.21875e1 // Decimal floating-point literal
let hexadecimalDouble = 0xC.3p0 // Hexadecimal floating-point literal
String Literals
String literals consist of a sequence of characters enclosed in double quotes, as follows:
"characters"
String literals cannot contain unescaped double quotes ("
), unescaped backslashes (\
), carriage returns, or linefeeds.
Escape Sequence | Meaning |
---|---|
\0 | Null character |
\ | Backslash \ |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\' | Single quote |
\" | Double quote |
\000 | Any character represented by a 1-3 digit octal number |
\xhh... | Any character represented by a 1-2 digit hexadecimal number |
Here is a simple example of a string literal:
import Cocoa
let stringL = "Hello\tWorld\n\ntutorialpro.org website: \'http://www.tutorialpro.org\'"
print(stringL)
The output of the above program is:
Hello World
tutorialpro.org website: 'http://www.tutorialpro.org'
Boolean Literals
The default type for boolean literals is Bool
.
Boolean literals have three values, which are reserved keywords in Swift:
true
represents true.false
represents false.nil
represents no value.