Constants and variables in Swift are briefly outlined

  • 2020-05-13 03:32:04
  • OfStack

1. It is stipulated in Swift 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 that cannot be modified after definition

3. Use var to define variables, which can be modified after definition

4. Define constants and variables

Constant: name of the let identifier: type = assignment

Variable: name of the var identifier: type = assignment

5. Capitalize the first letter of the type, and the names of constants and variables cannot contain mathematical symbols, arrows

6. Use of constants

6.1 in development,apple recommends that constants be used first and changed to variables only when it is necessary to change them, because constants are more secure and will not be changed arbitrarily

6.2 the nature 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 can be modified


*/
//  Define constants 
let a : Int = 9
//  Define variables 
var b : Int = 38
//  in 1 Multiple variables are defined in the line 
var red,green,brown : String
let 🐶 = 9
print(🐶)
// Constant and variable names cannot contain mathematical symbols, arrows 
//var 212a : Int = 23

The above content is this site to introduce you switf constants and variables, I hope to help you!


Related articles: