Swift learning-Swift BASICS (1)

Source: Internet
Author: User

Note: As the basic part has been roughly described in swift tour, the explanation does not reach 100% of the original meaning.


Constants and variables constants and variables

Both constants and variables need to declare names and types (as programmers, these basics will not be mentioned). A constant value cannot be changed at a time, and the value of a variable can be changed.



Declaring constants and variables declare constants and variables

Constants and variables must be declared before use. Use the let keyword to define constants and the VaR keyword to define variables.

The following example defines the maximum number of user logon attempts:

let maximumNumberOfLoginAttempts = 10var currentLoginAttempt = 0

Multiple variables or constants can be defined in one row, separated by commas:

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



Type annotations type flag

The Type flag (type comment) can mark the type of the variable or constant you define that can be stored. Place a colon after the name of the variable/constant, leave a space, and write the type name.

var welcomeMessage: String
This variable can store the string type.

welcomeMessage = "Hello"
In the actual encoding process, you almost do not need to mark the variable/constant type, because when you assign a value for the first time, swift already knows the type of the variable, if no value is assigned when a variable is declared, swift identifies the type of the variable through the type mark.



Naming constants and variables naming variables and constants

You can use any character you like to name variables and constants, including unicode encoding:

Let π = 3.14159let Hello = "", you have played so much white, Because I pasted a facial expression and wrote nothing in the back, so sang Xin.

In swift, the name definitions for constants and variables are much broader.

Constants and variables cannot contain mathematical symbols, arrows, private-defined Unicode characters, or hyphens. They cannot start with a number, however, the number can be elsewhere in the name.

Once you define a constant or variable with a definite type, you cannot define other variables with the same variable name or change the type of the variable or constant, the variable cannot be converted to a constant or a constant.

If you want to use the swift keyword as the variable name, your friends can only say (no Zuo no die why you try) to you. Since you insist on using this keyword, swift also provides the method you use to enclose variable names with parentheses, like this (')

Change the variable value:

var friendlyWelcome = "Hello!"friendlyWelcome = "Bonjour!"// friendlyWelcome is now "Bonjour!"
If the constant value cannot be changed, a compilation error is returned:

let languageName = "Swift"languageName = "Swift++"// this is a compile-time error - languageName cannot be changed


Printing constants and variables print constants and variables

Println can be used for printing:

println(friendlyWelcome)// prints "Bonjour!
Once I used oC, I know it. It is also available in C and C ++ with nslog, and it can also contain parameters.

The difference between println and print is that they are all used for console printing. println is used for line feed after printing and no line feed after printing.

You can also print a string:

println("This is a string")// prints "This is a string
Another way is to put the variable or constant in the string, put the variable in brackets (), and add the backslash \ in front, such as \ (variable. Constant)

println("The current value of friendlyWelcome is \(friendlyWelcome)")// prints "The current value of friendlyWelcome is Bonjour!


Comments

Single line comment

// this is a comment
Multi-line comment

/* this is also a comment,but written over multiple lines */
Nested comment:

/* this is the start of the first multiline comment/* this is the second, nested multiline comment */this is the end of the first multiline comment */

The function of nested comments is to annotate a large segment of code at a time without any additional comments in the code.



Semicolons semicolon

Swift code can be separated by semicolons (;). Each line is a statement. If two statements are required for one line, separate them with semicolons (;).

let cat = "cats"; println(cat)// prints "cats"


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.