Basic Types and Process Control of Swift, swift type Process

Source: Internet
Author: User

Basic Types and Process Control of Swift, swift type Process

Swift is a static, strong language. It is well known for its simplicity, cleanliness, and readability.

  

  

  

1. Why is it a static language?

First, static languages are more secure than dynamic languages. It does not allow automatic conversion between types and must be displayed for conversion. Swift is type-safe. It checks your code during compilation. If any type does not match, an error is returned. This allows programmers to quickly capture and correct errors during development as early as possible. However, this does not mean that you must specify the type declared by every constant and variable. If you do not specify the expected type, Swift uses type derivation to specify the corresponding type. Type derivation enables the compiler to automatically export the type of a specific expression through the initialization value you provide during compilation. Type derivation allows Swift to require fewer type declaration statements than C or Objective-C. Constants and variables are still explicit, but most of the work of specifying their types Swift has done for you. When you declare a constant or variable and give the initial value type, type derivation is particularly useful. This is usually done by assigning a constant value to the declared constant or variable.

  

For example, you need to declare a username string constant and assign a value to it as "json ".

Let userName = "json"

  

You do not need to specify any type. during compilation, the constant userName is automatically set to the type of the assigned value through the type derivation mechanism.

  

  

2. What is a strong language?

That is, variables or constants must be defined before they can be used. Otherwise, an error is reported.

 

Now let's get down to the point. Next we will introduce the basic types and Process Control of swift.

  

  

3. Basic Types

The options include integer Int, floating point Double, Float, Boolean Bool, and String. Swift also provides two more powerful basic collection data types: Array and Dictionary. It should be mentioned here. In addition to the basic data types, swift also has a data type called "yuanzu", which is unique to it. I will explain it later.

4. constants and variables

  

The naming of constants and variables is still based on the "hump" that everyone is familiar. A constant points to a value of a specific type. Once assigned, it cannot be modified again. A variable also points to a specific type of value, but it can be modified as needed. In addition, the purpose of swift is concise, so the semicolon is not allowed on each line.

As mentioned above, swift is a strongly typed language, so its constants and variables must be declared before use. How can constants and variables be declared?

Constant declaration:

Starting with let, it indicates that you want to declare a constant and then get the variable name. Generally, the variable name is named in the hump naming method, connected with the = symbol, and then the value you want to assign. If you do not specify the initial value, you must specify the constant type.

Example: let usreName = "tom" // assign a value when declaring

Let userName String // The value is not initialized when it is declared

UserName = "tom"

Note: Because the syntax of swift requires planning, no space is left between values and operators or spaces are left.

It is worth noting that the characters are different when being declared. Anyone who has learned other advanced languages knows that other advanced languages generally enclose the values in single quotes when declaring characters. However, swift is different, its character assignment still uses double quotation marks, but must be declared with annotations.

For example:

Var ch: Character = "c" // The Character value must be double quotation marks and a Character

Constants declared starting with let cannot be modified after a value is assigned.

  

 

Variable declaration:

Start with var. The rest are consistent with the declaration of constants. Here we will not describe them in detail. Let's refer to the declaration of constants.

 

5. Process Control

  

    

If esle:

  

For example:

  

Let I = 1

  

If I {

  

Print ("writing error! ")

}

  

For swift, this method is incorrect. I is a constant, not an expression, and cannot be used as a boolean type.

The correct compiling should be:

  

Let I = 1

  

 

If I = 1 {

 

  

 

Print ("written correctly! ")

 

}

  

 

For: there are two forms.

First: for example:

 

  

For var I = 0; I <1000; I ++ {

Print ("I love you one thousand times! ")

}

  

  

Example 2:

  

Let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For a in array {

Print ("\ (a),") // The output result is :,.

}

  

Compared with the first method, the second method is that you can traverse each element without knowing the length of the array.

 

While: it is also a loop control statement. Can be perfectly converted to. For example:

  

  

Var I = 0

  

  

While I <1000 {

  

Print ("I love you one thousand times! ")

I ++

}

Repeat while: a loop control statement, which has the same effect as do while in java, is executed first and then judged by the condition. For example:

  

Var I = 0

Repear {

 

Print ("I love you one thousand times! ")

I ++

 

 

} While I <1, 1000

 

 

Switch: Branch judgment statement. Some of our friends have also learned this before, but the role of switch in swift is even more powerful and prominent. It can receive Arbitrary Constants and variables, as long as the types before and after are consistent. For example:

Let I = "abc"

Switch I {

Case "abc ":

Print ("abc ")

Default:

Print ("Other cases ")

}

Note: Swift requires that the switch is a complete structure, that is, the switch must have more than one case and one default.

Now, today's sharing is here. I hope I can help my friends who have just learned ios or want to learn ios. I am also a newbie. If there are any shortcomings or mistakes, please correct me. Let's make progress together! Everyone can follow my weibo http://www.cnblogs.com/lb0129/

 

 

  

 

  

  

    

  

 

      

      

      

    

 

Related Article

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.