Basics:
- Constants and variables
- Note
- Semicolon
- Integer
- Floating Point Number
- Type security and type inference
- Numeric literal
- Numeric type conversion
- Type alias
- Boolean Value
- Tuples
- Optional
- Assertions
Declare constants and variables
Use let to declare constants, and use VaR to declare variables.
Type Annotation
When declaring a constant or variable, you can add type annotation to indicate the type of the value to be stored in the constant or variable.
If you want to add a type annotation, you need to add a colon and space after the constant or variable name, and then add the type name.
var welcomeMessage: String
Note: Generally, you rarely need to write type annotations. If you assign an initial value when declaring a variable or constant, Swift can infer the type of the variable or constant.
Constant and variable naming
Constants and variable names cannot contain mathematical symbols, arrows, reserved Unicode characters, connections, and tabs. It cannot start with a number, but it can contain numbers elsewhere in the constant and variable name.
Once you declare a constant and a variable as a definite type, you cannot declare it again with the same name or change the type of the stored value. At the same time, you cannot convert constants and variables.
Note:
If you need to use the same name as the reserved keyword of swift as the constant or variable name, you can use backquotes to enclose the keyword as the name.
Output constants and variables
The println function outputs the value of the current constant or variable:
Println (friendlywelcome) // output "Bonjour! "
Println is a global function used for output. The output content is wrapped in a line break.
Swift uses string interpolation to add a constant or variable name to a long string as a placeholder. Swift replaces these placeholders with the values of the current constant or variable. Place the constant or variable name in parentheses and escape it with a backslash before the parentheses:
Println ("the current value of friendlywelcome is \ (friendlywelcome)") // output "the current value of friendlywelcome is Bonjour!