SoFunction
Updated on 2025-04-10

A simple overview of constants and variables in Swift

1. Swift stipulates that when defining an identifier, it must be clearly stated whether the identifier is a constant or a variable.

2. Use let to define constants, and cannot be modified after definition.

3. Use var to define variables, and you can modify them after definition.

4. Define constants and variables

Constant: let identifier name: Type = Assignment

Variable: var identifier name: Type = Assignment

5. The first letter of the type must be capitalized, constants and variable names cannot contain mathematical symbols, arrows

6. Pay attention to the use of constants

6.1 In development, Apple recommends that constants be used first, and only when they are found to be modified will be changed to variables, because constants are safer and will not be modified arbitrarily.

6.2 The essence of constants. The memory address pointed to cannot be modified, but the object itself can be obtained through the memory address, and then the properties inside the object are modified.

*/
// Define constantslet a : Int = 9
// Define variablesvar b : Int = 38
// Define multiple variables in one linevar red,green,brown : String
let 🐶 = 9
print(🐶)
//Constant and variable names cannot contain mathematical symbols, arrows//var 212a : Int = 23

The above content is the constants and variables in switf introduced to you by the editor. I hope it will be helpful to you!