Swift literals
The so-called literal refers to a value such as a specific number, string, or Boolean value that can directly indicate its type and assign a value to a variable. For example, below:
let aNumber = 3 //整型字面量 let aString = "Hello" //字符串字面量 let aBool = true //布尔值字面量
Integer literal
An integer literal can be a decimal, binary, octal or hexadecimal constant. The binary prefix is 0b, the octal prefix is 0o, the hexadecimal prefix is 0x, and the 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 Type literal
Floating point literal has an integer part, a decimal point, a decimal part and an exponent part.
Unless otherwise specified, the default derivation type of floating-point literals is Double in the Swift standard library type, which represents a 64-bit floating point number.
Floating point literals are expressed in decimal by default (without prefix), and can also be expressed in hexadecimal (with the prefix 0x).
A decimal floating-point literal consists of a decimal digit string followed by a decimal part or an exponent part (or both). The decimal part consists of a decimal point . followed by a string of decimal digits. The exponent part consists of an uppercase or lowercase letter e prefixed by a string of decimal digits that represents the power of 10 multiplied by the quantity before e. For example: 1.25e2 means 1.25 ⨉ 10^2, which is 125.0; similarly, 1.25e-2 means 1.25 ⨉ 10^-2, which is 0.0125.
A hexadecimal floating-point literal consists of a prefix 0x followed by an optional hexadecimal fractional part and a hexadecimal exponent part. The hexadecimal fraction consists of a decimal point followed by a string of hexadecimal digits. The exponent part consists of an uppercase or lowercase letter p prefixed by a string of decimal digits that represents the power of 2 multiplied by the quantity preceding p. For example: 0xFp2 means 15 ⨉ 2^2, which is 60; similarly, 0xFp-2 means 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. Underscores will be ignored by the system and therefore will not affect the value of the literal. Similarly, you can add 0 in front of a number and it will 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 are enclosed in double quotes It consists of a string of characters in the following format:
"characters"
String literals cannot contain unescaped double quotes ("), unescaped backslashes (\), carriage returns or line feeds symbol.
Transfer characters | Meaning |
---|---|
\0 | Null character |
\\ | Backslash\ |
\b | Backspace (BS), moves the current position to the previous column |
\f | Page change (FF), moves the current position to the beginning of the next page |
\n | Line feed character |
\r | Carriage return character |
\t | Horizontal tab Symbol |
\v | Vertical tab character |
Single quotation mark | |
Double quotes | |
Any character represented by 1 to 3 octal numbers | |
Any character represented by 1 to 2 hexadecimal digits |
import Cocoa let stringL = "Hello\tWorld\n\nphp中文网官网:\'http://www.php.cn\'" print(stringL)The execution result of the above program is:
Hello World php中文网官网:'http://www.php.cn'
Boolean literalThe default for Boolean literal The type is Bool. Boolean literals have three values, which are Swift’s reserved keywords:
- #true
means true.
false - means false.
##nil
means none. value.