Constants and variables

Source: Internet
Author: User

Constants and variables

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

declaring constants and variables

Constants and variables must be declared before use, declared with let constants, and used var to declare variables. The following example shows how to use constants and variables to record the number of times a user attempts to log on:

let Number = 10var currentLoginAttempt = 0

These two lines of code can be understood as:

"Declares a new constant with a name Number , and gives it a value 10 . Then, declare a variable with a name currentLoginAttempt and initialize its value to 0 . "

In this example, the maximum number of attempted logins allowed is declared as a constant, because this value does not change. The current number of attempts to log in is declared as a variable, because this value needs to be incremented each time an attempt to log on fails.

You can declare multiple constants or multiple variables in a single line, separated by commas:

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

Attention:
If you have a value in your code that does not need to be changed, let declare it as a constant using the keyword. Only the values that need to be changed are declared as variables.

Type callout

When you declare a constant or variable, you can add a type callout (type annotation), indicating the type of the value to be stored in a constant or variable. If you are adding a type callout, you need to add a colon and a space after the constant or variable name, and then add the type name.

This example Message adds a type callout to the variable, indicating that the variable can store String the value of the type:

var Message: String

The colon in the declaration represents "Yes ... Type ", so this line of code can be understood as:

"Declares a String variable of type, first name Message . ”

"Type is String " means "any String type of value can be stored." ”

MessageVariables can now be set to any string:

Message = "Hello"

You can define multiple variables of the same type in a row, split with commas, and add type callouts after the last variable name:

var red, green, blue: Double

Attention:
Generally, you rarely need to write type callouts. 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 initial value is not assigned Message , so the type of the variable Message is specified by a type callout, not by the initial value.

Names of constants and variables

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

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

Constants and variable names cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, lines and tabs. You cannot start with a number, but you can include a number elsewhere in a constant and variable name.

Once you declare a constant or variable as a deterministic type, you cannot declare it again with the same name, or change the type of value it stores. At the same time, you cannot turn constants and variables into each other.

Attention:
If you need to use the same name as the SWIFT reserved keyword as a constant or variable name, you can use the inverse quotation mark (') to enclose the keyword in a way that uses it as a name. In any case, you should avoid using keywords as constants or variable names, unless you have no choice.

You can change the value of an existing variable to another value of the same type, in the following example, friendlyWelcome the value is "Hello!" changed from "Bonjour!" :

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

Unlike variables, the value of a constant cannot be changed once it is determined. Trying to do this causes the compile times to be wrong:

let languageName = "Swift"languageName = "Swift++"// 这会报编译时错误 - languageName 不可改变

Output Constants and variables

You can use print(_:separator:terminator:) a function to output the value of the current constant or variable:

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

print(_:separator:terminator:)is a global function used to output one or more values to the appropriate output area. If you use Xcode, the print(_:separator:terminator:) content will be output to the console panel. separatorand terminator parameters have default values, so you can ignore them when you call this function. By default, the function ends the current line by adding a newline character. If you do not want to wrap, you can pass an empty string to the terminator parameter--for example, print(someValue, terminator:"") .

Swift uses string interpolation (string interpolation) to add a constant name or variable name as a placeholder to a long string, and Swift replaces the placeholder with the value of the current constant or variable. Enclose the constant or variable name in parentheses and escape it with a backslash before opening the parentheses:

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

Constants and variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.