Latest web development tutorials

Swift literal

The so-called literal, refers to as a specific number, string, or Boolean value this can be pointed directly to the local own type and value for the variable assignment. For example, in the following:

let aNumber = 3         //整型字面量
let aString = "Hello"   //字符串字面量
let aBool = true        //布尔值字面量

Integer literals

Integer literals can be a decimal, binary, octal or hexadecimal constants. Binary prefix 0b, octal prefix 0o, hexadecimal prefix 0x, decimal values ​​have no prefix:

Following is some examples of integer literals:

let decimalInteger = 17           // 17 - 十进制表示
let binaryInteger = 0b10001       // 17 - 二进制表示
let octalInteger = 0o21           // 17 - 八进制表示
let hexadecimalInteger = 0x11     // 17 - 十六进制表示

Float literals

Float literals have an integer part, a decimal point, and the fractional part of an exponent part.

Unless otherwise specified, the default type of floating-point literal derivation of Swift standard library types Double, 64-bit floating-point representation.

Float literals default decimal representation (no prefix), you can also use hexadecimal (prefix 0x).

Decimal floating-point literal string of digits followed by a decimal or fractional part of an exponent part (or both) components. Decimal part of the decimal point followed by a decimal digit string components. Exponent part by the uppercase or lowercase letter e is prefix followed by a decimal digit string consisting of this string of numbers represents the number of e multiplied several times before the parties 10. For example: 1.25e2 represents 1.25 ⨉ 10 ^ 2, that is, 125.0; similarly, 1.25e-2 represents 1.25 ⨉ 10 ^ -2, which is 0.0125.

Composition hexadecimal floating-point literals by the prefix 0x followed by an optional decimal part and hexadecimal hexadecimal exponent part. Hexadecimal fractional part of a decimal point followed by a string of hexadecimal numbers composed. Exponent part by the uppercase or lowercase letters p as a prefix followed by a decimal digit string composed of string which indicates the number of p multiplied several times before the party 2. For example: 0xFp2 represents 15 ⨉ 2 ^ 2, that is, 60; similarly, 0xFp-2 represents 15 ⨉ 2 ^ -2, which is 3.75.

Negative floating-point literal consists of a unary minus - and floating-point literals composed, for example, -42.5.

Floating-point literal underscore _ to allow the use of enhanced digital readability, underscores are ignored by the system, it will not affect the value of the literal. Similarly, you can also add 0 before the number, and 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

By a string literal is enclosed in double quotation marks in a string of characters, the form is as follows:


String literals can not contain unescaped double quotes ( "), did not escape the backslash (\), a carriage return or line feed.

Character Transfer meaning
\ 0 Null character
\\ Backslash \
\ B Backspace (BS), the current position to the previous one
\ F Form feed (FF), the current position to the beginning of the next page
\ N Newline
\ R Carriage return
\ T Horizontal tab
\ V Vertical tab
\ ' apostrophe
\ " Double quotes
\ 000 Any character 1-3 octal number represented
\ Xhh ... 1-2 hexadecimal character represents any

Following is a simple example of a string literal:

import Cocoa

let stringL = "Hello\tWorld\n\n本教程官网:\'\'"

The above program execution results:

Hello	World


Boolean Literals

The default type Boolean literal is Bool.

Boolean literal value has three values, which are reserved keywords Swift:

  • true true representation.

  • false false representation.

  • nil means no value.