Swift Literals
A so-called literal is a value that can directly indicate its own type and be assigned to a variable, such as a specific number, string, or boolean value. For example, in the following:
let aNumber = 3 //整型字面量 let aString = "Hello" //字符串字面量 let aBool = true //布尔值字面量
Integer Literals
Integer literals can be decimal, binary, octal, or hexadecimal constants. The binary prefix is 0b, the octal prefix is 0o, the hexadecimal prefix is 0x, and decimal has no prefix:
The following are some examples of integer literals:
let decimalInteger = 17 // 17 - 十进制表示 let binaryInteger = 0b10001 // 17 - 二进制表示 let octalInteger = 0o21 // 17 - 八进制表示 let hexadecimalInteger = 0x11 // 17 - 十六进制表示
Floating-Point Literals
Floating-point literals have an integer part, a decimal point, a fractional part, and an exponent part.
Unless otherwise specified, the default inferred type of a floating-point literal is Double in the Swift standard library, representing a 64-bit floating-point number.
Floating-point literals are represented in decimal by default (no prefix), and can also be represented in hexadecimal (with the prefix 0x).
Decimal floating-point literals consist of a decimal digit string followed by a fractional part, an exponent part, or both. The decimal fractional part consists of a decimal point . followed by a decimal digit string. The exponent part is prefixed by an uppercase or lowercase letter e and followed by a decimal digit string, which represents the quantity before e multiplied by 10 raised to the power of that number. 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 the prefix 0x followed by an optional hexadecimal fractional part and a hexadecimal exponent part. The hexadecimal fractional part consists of a decimal point followed by a hexadecimal digit string. The exponent part is prefixed by an uppercase or lowercase letter p and followed by a decimal digit string, which represents the quantity before p multiplied by 2 raised to the power of that number. 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 consist of the unary operator minus sign - and a floating-point literal, for example -42.5.
Floating-point literals allow the use of underscores _ to enhance the readability of numbers. The underscores are ignored by the system and therefore do not affect the value of the literal. Similarly, you can add 0 before the number, and it does not affect the value of the literal.
The following are some examples of floating-point literals:
let decimalDouble = 12.1875 //十进制浮点型字面量 let exponentDouble = 1.21875e1 //十进制浮点型字面量 let hexadecimalDouble = 0xC.3p0 //十六进制浮点型字面量
String Literals
String literals consist of a sequence of characters enclosed in double quotation marks, in the following form:
"characters"
String literals cannot contain unescaped double quotation marks ("), unescaped backslashes (\), carriage return characters, or line feed characters.
| Escape character | Meaning |
|---|---|
| \0 | Null character |
| \\ | Backslash \ |
| \b | Backspace (BS), moves the current position to the previous column |
| \f | Form feed (FF), moves the current position to the beginning of the next page |
| \n | Line feed |
| \r | Carriage return |
| \t | Horizontal tab |
| \v | Vertical tab |
| \' | Single quotation mark |
| \" | Double quotation mark |
| \000 | Any character represented by an octal number of 1 to 3 digits |
| \xhh... | Any character represented by a hexadecimal number of 1 to 2 digits |
The following are simple examples of string literals:
import Cocoa let stringL = "Hello\tWorld\n\nExample官网:\'http://www.example.com\'" print(stringL)
The output of the above program execution is:
Hello World Example官网:'http://www.example.com'
Boolean Literals
The default type of a boolean literal is Bool.
Boolean literals have three values, which are Swift's reserved keywords:
trueRepresents true.
falseRepresents false.
nilRepresents no value.