Zhao Yazhi _swift (2) _swift constants and variables

Source: Internet
Author: User

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 the variable can be changed arbitrarily.

declaring constants and variables

constants and variables must be declared before use, using the let to declare constants, declare variables with VAR .

The following example shows how to use constants and variables to record the number of times a user attempts to log on:

These two lines of code can be interpreted as: declaring a new constant with a name of maximumnumberofloginattempts and giving it a value of 10. Then, declare a variable whose name is 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

Note: If you have a value in your code that does not need to be changed, use the LET keyword to declare it as a constant. 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 adds a type callout to the Welcomemessage variable, indicating that the variable can store a value of type string:

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

"Declares a variable of type string, with the name Welcomemessage. ”。 "Type string" means "a value of any string type can be stored." ”

The Welcomemessage variable can now be set to any string:


Note: In general, 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 , please refer to type safety and type inference. In the above example, Welcomemessage is not assigned an initial value, so the type of the variable welcomemessage 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:

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 .

Note: 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, the value of Friendlywelcome from "hello!" Changed to "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:


output constants and variables

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


PRINTLN is a global function to output, and the output will be wrapped in the last line. If you use XCODE,PRINTLN, the content will be output to the console panel. (another function is called print, the only difference is that the output will not be wrapped at the end of the line.) )

The PRINTLN function outputs the string value passed in:


Similar to the NSLog function in Cocoa, the println function can output more complex information. This information can contain the values of the current constants and variables.

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 :

Note: string interpolation for all available options, refer to string interpolation .

Zhao Yazhi _swift (2) _swift 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.