Swift Data type:

Basic data types: Int Double and Float Floating point Bool Bool String Text

Set type: Array Array Set Dictionary Dictionary

Higher-order data type: Tuple Tuple

Optional type Optional

var username = "frankphper"Copy the code

One line of code declares multiple variables

var email = "[email protected]", github = "https://github.com/weirubo"Copy the code

Type annotation

var url: String
url = "http://www.php-z.com/?200052"Copy the code

One line of code defines multiple variables of the same type

Values that do not need to be changed are declared as constants with let. Values that need to be changed are declared as variables using var.

Naming constants and variables

Names cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, lines, or tabs.

Names cannot start with a number.

If a constant or variable is declared as a definite type, it cannot be declared again with the same name or its stored worth type changed. At the same time, constants and variables cannot be interrotated.

You can change the value of an existing variable to another value of the same type, as follows:

var name = "zhangsan"
name = "lisi"Copy the code

Unlike variables, the value of a constant cannot be changed.

Output constants and variables

Use the print() function to print the value of a constant or variable as follows:

Var uname = "zhangsan" print(uname) // By default this function ends the line by adding a newline characterCopy the code

If you don’t want a line break, you can pass an empty string to the terminator argument as follows:

let price = 1_000_000
print(price, terminator:"")Copy the code

// Single-line comment

/* Multi-line comments

Multi-line comments */

/* Multi-line comments

/* Nested comments */

Multi-line comments */

Swift does not enforce a semicolon at the end of every statement, although it is possible to add a semicolon at its own convenience. There is, however, one case where you must use a semicolon, where you write multiple separate statements on the same line. As follows:

var myname = "zhangsan"; print(myname)Copy the code