Swift literally

Literally, it refers to values like a specific number, string, or Boolean value that can directly point out your own type and assign values to variables. For example, here's what:

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

The literal amount of the integer

The literal quantity of an integer can be a heteth, binary, octal, or heteen constant. T he binary prefix is 0b, the octal prefix is 0o, the hex prefix is 0x, and the octal has no prefix:

Here are some examples of integer literal quantities:

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

Floating-point literal quantity

Floating-point literal quantities have integer parts, adget points, small parts and exponential parts.

Unless specifically specified, the default inference type for floating-point literal quantities is Double in the Swift standard library type, which represents a 64-bit floating-point number.

Floating-point literals are expressed by default in a hex (no prefix) or hex (with a prefix 0x).

A hex floating-point literal quantity consists of a string of hex digits followed by a small part or an exponential part (or both). T he hemissal component consists of a few points . C onsists of a string of hedding numbers. T he exponential portion consists of a string of prefixes followed by the lowercase letter e, which represents the number before e multiplied by several squares of 10. For example, 1.25e2 represents 1.25 ⨉ 10^2, or 125.0, and 1.25e-2 means 1.25 ⨉10^-2, or 0.0125.

Hex floating-point literal quantity consists of the prefix 0x followed by the optional hexaad component and the hex index portion. T he heteered part consists of a string of hetelude points followed by hete sixteen-digit numbers. T he exponential portion consists of a string of prefixes followed by the lowercase p prefix, which represents the number before p multiplied by several squares of 2. F or example, 0xFp2 represents 15 ⨉ 2^2, or 60, and 0xFp-2 means 15 ⨉ 2^-2, or 3.75.

Negative floating-point literals consist of a unit operator minus sign - and floating-point literals, such as -42.5.

Floating-point literals allow the use of underscores to enhance the readability of numbers, and underscores are ignored by the system, so they do not affect literal values. S imilarly, you can add 0 before the number without affecting the literal value.

Here are some examples of floating-point literal quantities:

let decimalDouble = 12.1875       //十进制浮点型字面量
let exponentDouble = 1.21875e1    //十进制浮点型字面量
let hexadecimalDouble = 0xC.3p0   //十六进制浮点型字面量

String literally

String literally consists of a string of characters wrapped in double quotes in the form of the following:

"characters"

String literals cannot contain un escaped double quotes ("), un escaped anti-slashes (\), carriage returns, or line breaks.

Transfer characters Meaning
\0 Empty character
\\ Backslashes . . .
\b Drop back (BS) and move the current position to the previous column
\f Page change (FF), move the current position to the beginning of the next page
\n Line breaks
\r Carriage return
\t Horizontal tabs
\v Vertical tabs
\' Single quotes
\" Double quotes
\000 Any character represented by a 1- to 3-bit octal number
\xhh... Any character represented by 1 to 2 hens

Here's a simple example of a string literally:

import Cocoa

let stringL = "Hello\tWorld\n\nW3Cschool教程官网:\'http://www.w3cschool.cn\'"
print(stringL)

The above procedures are performed as follows:

Hello	World

W3Cschool教程官网:'http://www.w3cschool.cn'

Boolean literal quantity

The default type for Boolean literal quantities is Bool.

Boolean values literally have three values, which are Swift's reserved keywords:

  • true means true.

  • false indicates false.

  • nil indicates that there is no value.