Constants and variables

Source: Internet
Author: User

Constants and variables have a name that associates a particular type of value. Once the value of a constant is set, it cannot be changed, and then the value of the variable is set, and a different value can be set after the inverse.

Defining Constants and variables

Constants and variables must be declared before they are used. You can declare a constant with the LET keyword and declare a variable with the var keyword. Here's an example of how to track a user's login count with constants and variable records

Let maximumnumberofloginattempts = 10 (maximum number of logins logged with a constant)

var currentloginattempt = 0 (with variable current user logon count)

This code analysis is as follows:

Declares a new constant called maximumnumberofloginattempts and sets it to a value of 10. It then declares a new variable called Currentloginattempt and sets an initial value of 0.

In this example, the maximum number of attempts to log on is declared as a constant, because the number of logons is never changed. The current attempt to log in is declared as a variable, because the value must be increased by 1 after each attempt to log on.

You can also declare multiple constants and multiple variables within the same line, separated by commas.

For example: var x = 0.0, y = 0.0, z = 0.0

Attention:

If some of the values in your code do not change, always use the Let keyword to create a constant to store. Use variables to store a number of values that need to be changed.

Type Comment

You can provide a type limit when declaring a constant and variable, and you can clearly see what kind of value this constant and variable can store. By adding a colon (:) behind a constant or variable name and writing a specific type comment behind the colon

The example below provides a type comment for a variable called Welcomemessage, which indicates that the variable is used to store a string value.

var welcomemessage:string

The colon in this statement means "... The type is ... ", so the above code means:

"declares a variable named Welcomemessage that is of type string."

This notation means that only string values can be stored.

The Welcomemessage variable can now set any value of a string type without errors

Welcomemessage = "Hello"

You can also define more than one type of variable in the same row, separated by commas to them, add a type comment after the last variable name:

var red,green,bule:double

Note: In practice, you rarely need to write type annotations. If you provide an initial value when you define a variable or constant, Swift can always infer the type of the constant or variable, as described in the type safety and type inference sections. In the Welcomemessage example, the initial value is not provided, so the variable welcomemessage indicates that the specific type annotation is not as good as the initial value to infer automatically.

Names of constants and variables

The names of constants and variables can contain any character and Unicode characters

Let's, pi? =? 3.14159
? How are you? = ?" Hello World "
? Let??????? = ?" Dogcow "

Constants and variable names cannot contain space characters, numeric symbols, arrows, private user Unicode points, lines, and box drawing characters. It cannot start with a number, and then the number can be contained anywhere in the name.

Once you have defined a constant or variable of a certain type, you will not be repeating a constant or variable with the same name, or change it with a different type of value. You cannot change a constant to a variable or change a variable to a constant.

Note: If you need to put a constant or a variable name in the same way as Swift's reserved keyword, you need to enclose the name in parentheses (') as a constant or variable name. In any case, avoid using keywords as constants or variable names unless you really don't have a better choice.

You can change a value that already exists for a variable with the same value as another type. In the example below, the value of Friendlywelcome is from "hello!" Change to "bonjour!":

var friendlywelcome = "Hello!"

Friendlywelcome = "bonjour!"

Now the value of Friendlywelcome is "bonjour!"

Unlike variables, the value of a constant cannot be changed once it is set. Attempting to change the value of a constant will report an error when your code is compiled:

Let LanguageName = "Swift"

LanguageName = "swift++"

This is a compile-time error--languagename cannot be changed

Printing constants and variables

You can use the PRINTLN function to print out the current value of a variable or constant:

println (Friendlywelcome)

Print out "bonjour!"

PRINTLN is a global function for printing output values and outputting a newline. In Xcode, for example, println outputs results in a panel called the console ("console"). (another function is called print, and the same task as Printl does is to simply not append line jumps at the end of the printed value)

The println function can print out any string you pass to him:

println ("This is a string")

Print output "This is a string"

The println function is capable of printing more complete record information, similar to the style of the cocoa nslog function. These messages can contain the current value of a constant or variable.

Swift can use and support some strings that insert the name of a constant or variable as a placeholder to prompt Swift to replace the corresponding place with the current value of a constant or variable. Place the name of the constant or variable in a small back bracket () and a backslash "\" before the parentheses () to construct a placeholder.

println ("The current value of Friendlywelcome is \ (friendlywecome)")

Print output "The current value of Friendlywelcome is bonjour!"

Note: All options for interpolation of strings are explained and explained in detail in the "String insertion" subsection.

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.