Swift literal quantity


Release date:2023-11-24 Update date:2023-12-08 Editor:admin View counts:130

Label:

Swift literal quantity

A literal quantity is a value such as a specific number, string, or Boolean value that can directly indicate its own type and assign a value to a variable. For example, in the following:

let aNumber = 3         //Integer literal quantity
let aString = "Hello"   //string literal
let aBool = true        //Boolean literal

Integer literal quantity

The 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:

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 literal quantity

Floating-point literals have integer parts, decimal points, decimal parts and exponential parts.

Unless otherwise specified, the default derivation type for floating point literals is the one in the Swift standard library type Double that represents a 64-bit floating-point number

Floating-point literals are represented by default in decimal (no prefix) orin hexadecimal (with prefix 0x).

A decimal floating-point literal consists of a decimal number string followed by a decimal or exponential part (or both). The decimal part consists of the decimal point. Followed by a decimal number string. The exponential part consists of an uppercase or lowercase e prefixed by a string of decimal numbers that represent the number before e times the powerof 10. For example, 125 means 1.25 to 10 ^ 2, which is 125.0; similarly, 1.25e-2 means 1.25 to 10 ^-2, which is 0.0125.

The hexadecimal floating-point literal consists of the prefix 0x followed bythe optional hexadecimal decimal part and the hexadecimal index part. The hexadecimal decimal part consists of a string of hexadecimal numbers followed by a decimal point. The exponential part consists of an uppercase or lowercase p prefixed by a string of decimal numbers that represent the number before p multiplied by the power of 2. 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 floating-point literals, such as-42.5.

Floating-point literals allow underscores _ To enhance the readability of numbers, underscores are ignored by the system, so the literal value is not affected. Similarly, you can add 0 to the number without affecting the literal 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 literal quantity

A string literal consists of a string of characters enclosed in double quotes in the following form:

"characters"

String literals cannot contain unescaped double quotes (“), unescaped backslashes (), carriage returns, or newline characters.

Escape character

Meaning

\0

Null character

\\

Backslash

\b

BS to move the current position to the previous column

\f

FF to move the current position to the beginning of the next page

\n

Newline character

\r

Carriage return symbol

\t

Horizontal tab character

\v

Vertical tab character

\'

Single quotation mark

\"

Double quotation marks

\000

Any character represented by a 1 to 3 octal number

\xhh…

Any character represented by 1 to 2 digits in hexadecimal

The following is a simple example of the literal amount of a string:

import Cocoa

let stringL = "Hello\tWorld\n\nCainiao Tutorial Official Website:\'http://www.runoob.com\'"
print(stringL)

The execution results of the above procedures are as follows:

Hello    World

Novice Tutorial official website:'http://www.runoob.com'

Boolean literal quantity

The default type of Boolean literals is Bool.

Boolean literals have three values, which are reserved keywords for Swift:

  • True

Powered by TorCMS (https://github.com/bukun/TorCMS).