Swift basic constants and variables

Constants and variables associate a name (such as maximumNumberOfLoginAttempts or welcomeMessage) with a value of a specified type (such as the number 10 or the string "Hello"). The value of a constant cannot be changed once it is set, and the value of a variable can be changed at will.

One: Declare constants and variables

Constants and variables must be declared before use. Let is used to declare constants and var is used to declare variables. The following example shows how to use constants and variables to record the number of user login attempts:

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

These two lines of code can be understood as:

"Declare a new constant named maximumNumberOfLoginAttempts and give it a value of 10. Then, declare a variable named currentLoginAttempt and initialize its value to 0."

In this example, the maximum allowed number of login attempts is declared as a constant, because this value will not change. The current number of login attempts is declared as a variable, because this value needs to be increased every time a login attempt fails.

You can declare multiple constants or multiple variables on one line, separated by commas:

var x = 0.0, y = 0.0, z = 0.0

Note : If there is a value in your code that does not need to be changed, please use the let keyword to declare it as a constant. Only declare the value that needs to be changed as a variable.

Two: Type label

When you declare a constant or variable, you can add a type annotation to indicate the type of value to be stored in the constant or variable. If you want to add a type annotation, you need to add a colon and a space after the constant or variable name, and then add the type name.

This example adds a type annotation to the welcomeMessage variable, indicating that this variable can store a value of type String:

var welcomeMessage: String

The colon in the declaration stands for "is...type", so this line of code can be understood as:

"Declare a variable of type String and name welcomeMessage."

"The type is String" means "any String type value can be stored."

The welcomeMessage variable can now be set to any string:

welcomeMessage = "Hello"

Note : Generally speaking, you rarely need to write type annotations. If you assign an initial value when declaring a constant or variable, Swift can infer the type of the constant or variable. In the above example, the welcomeMessage is not assigned an initial value, so the type of the variable welcomeMessage is specified through a type annotation, rather than inferred from the initial value.

Three: the naming of constants and variables

You can use any character you like as constant and variable names, including Unicode characters:

let π = 3.14159
let 你好 = "你好世界"
let ???????? = "dogcow"

Constants and variable names cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, connections, and tabs. It also cannot start with a number, but you can include numbers in other places in constants and variable names.

Once you declare a constant or variable as a certain type, you cannot declare it again with the same name or change the type of its stored value. At the same time, you cannot convert between constants and variables.

Note : If you need to use the same name as a reserved keyword in Swift as a constant or variable name, you can use it as a name by enclosing the keyword with backticks (`). In any case, you should avoid using keywords as constant or variable names unless you have no choice.

You can change the existing variable value to other values ​​of the same type. In the following example, the value of friendlyWelcome is changed from "Hello!" to "Bonjour!":

var friendlyWelcome = "Hello!"
friendlyWelcome = "Bonjour!"
// friendlyWelcome 现在是 "Bonjour!"

Unlike variables, the value of a constant cannot be changed once it is determined. Attempting to do so will result in an error at compile time:

let languageName = "Swift"
languageName = "Swift++" // Cannot assign to value: 'languageName' is a 'let' constant
// 这会报编译时错误 - languageName 不可改变

Four: output constants and variables

You can use the print function to output the value of the current constant or variable:

print(friendlyWelcome)
// 输出 "Bonjour!"

The print function outputs the incoming String value:

print("This is a string")
// 输出 "This is a string"

Swift uses string interpolation to add constant or variable names as placeholders to long strings. Swift will replace these placeholders with the current constant or variable value. Put the constant or variable name in parentheses and use a backslash before the opening parenthesis to escape it:

print("The current value of friendlyWelcome is \(friendlyWelcome)")
// 输出 "The current value of friendlyWelcome is Bonjour!

Note : all available options for string interpolation

Welcome to follow the official account [Swift Community]:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36478920/article/details/103453883