02_swift Constants and Variables + notes + semicolons

Source: Internet
Author: User

1. Constants and variables

Constants and variables take a name ( such as ' number ' or ' welcomemessage ') and A value of a specified type (such as the digit ' 10 ' or the string ' "Hello") Associated . the value of a constant cannot be changed once it is set , and The value of the variable can be changed arbitrarily.  

1> declaring variables and constants

  constants and variables must be declared before use , declare constants with ' let ', and declare variables with 'var'. The following example shows how to use constants and variables to make a number:

Let Constantsnumber = 10

var variablesnumber = 0

These two lines of code can be interpreted as: "Declare a new constant whose name is 'constantsnumber ' , and give it a value of ' 10 '." Then, declare a variable whose name is 'variablesnumber' and initialize its value to ' 0 ' .

You can declare multiple constants or multiple variables in a single row , 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.

2> Type Callout 

When you declare a constant or variable , you can add the _ type callout (type annotation)_, indicating the type of the value to be stored in the 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, which indicates that the variable can store a value of type 'String':
var welcomemessage: String

The colon in the declaration represents " Yes ... Type ", so this line of code can be understood as:" Declare a variable of type ' String ' with the name ' Welcomemessage '. "

A type of 'string' means ' a value of any 'string' type can be stored. "

The ' welcomemessage ' variable can now be set to any string:

Welcomemessage = "Hello"

> Note : 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 , please refer to [ type safety and type inference ] ( Type_safety_and_type_inference). In the above example, the ' welcomemessage ' is not assigned an initial value , so the type of the variable ' welcomemessage ' is specified by a type callout , not inferred by the initial value .

3> the names of constants and variables

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

let π= 3.14159

Let's hello = "Hello World"

Let???? = "Dogcow"

   Naming conventions for constants and variable names: cannot contain mathematical symbols , arrows , Reserved (or illegal) Unicode code bits , lines and tabs . You cannot start with a number , but you can include a number elsewhere in a constant and variable name.

  variable and constant names cannot be duplicated! 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 the same way as the 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! '. `:

var friendlywelcome = "hello!"

Friendlywelcome = "bonjour!"

Friendlywelcome is now "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++"

This will report a compile-time error-LanguageName cannot be changed

4> output constants and variables

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

Print (Friendlywelcome)

Output "bonjour!"

① 'print' is a global function to output, and the output will be wrapped in the last line . If you use Xcode, 'print' will output content to the ' console ' panel.

The ② 'print' function outputs the incoming ' String ' value:

    Print ("This is a string")

Output "This is a string"

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

Swift uses the string interpolation (string interpolation)_ to add the constant name or variable name as placeholders to the long string , Swift These placeholders are replaced with the values of the current constant or variable. Enclose the constant or variable name in parentheses and escape it with a backslash before opening the parentheses :

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

Output "The current value of Friendlywelcome is bonjour!

> Note : string interpolation for all options available, refer to [string interpolation] (03_strings_and_characters.html#string_interpolation).

the relationship between 5> constants and variables
    • Change is immutable. In any case, there are always rules to follow.

    • variables and constants can be converted to each other under certain conditions .

    • A constant is one of the variables .

2. Notes

comment The non-executable text in your code into hints or notes to make it easier for you to read in the future. The Swift compiler will automatically ignore the comment section when compiling the code .

1> Types of annotations

The comments in Swift are very similar to those in the C language. a single-line comment is a start tag with a double-forward slash ('//') :

This is a comment

You can also make multi-line comments with a single forward slash followed by an asterisk ('/*'), and a single forward slash (' * * ') followed by a stop marked with an asterisk ('* *'):

/* This is a,

Multi-line Comment */

Unlike the C-language multiline annotation, Swift's multiline comments can be nested within other multiline annotations. You can make a multi-line comment block, and then nest the second multiline comment in the comment block. When terminating a comment, insert the terminating tag of the second comment block, and then insert the terminating tag of the first comment block:

/* This is the beginning of the first multi-line comment

/* This is the second nested multiline comment */

This is the end of the first multi-line comment */

 By using nested multiline annotations, you can quickly and easily comment out a large chunk of code, even though the code already contains multiple lines of comment blocks .

The role of 2> annotations

    • Help yourself to understand

    • Easy for others to understand

    • Professional Elegance

3. Semicolon  

Unlike most other programming languages, Swift does not force you to use the semicolon (';') at the end of each statement , and you can, of course, add semicolons to your own custom. There is a case where you have to use a semicolon , that is, you intend to write multiple separate statements in the same line :

Let cat = "??"; println (CAT)

Output "??"

02_swift Constants and Variables + notes + semicolons

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.