Basic types and Process control for Swift

Source: Internet
Author: User

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

  

  

  

1, why is static language?

First, the static language is more secure than the dynamic language. It does not allow conversions between types that must be displayed automatically. Swift is type-safe and it will check your code at compile time, and any type mismatch will cause an error. This allows programmers to capture and remediate errors as early as possible in the development process. However, this does not mean that you must specify each constant and the type declared by the variable. If you do not specify the type you want,Swift uses type deduction to specify the appropriate type. Type deduction allows the compiler to automatically derive the type of a particular expression at compile time by the initialization value you provide. type deduction makes Swift require fewer type declaration statements than C or objective-c. Constants and variables are still explicitly typed, but most of the work that specifies their type is Swift already done for you. type deduction is especially useful when you declare a constant or variable and give the initial value type. This is usually done by assigning constant values to the declared constants or variables.

  

For example, you would declare a user-name string constant and assign him a value called "JSON."

Let UserName = "json"

  

You do not have to specify what type, at compile time, the constant username is automatically set to the type of the assigned value through the mechanism of type deduction.

  

  

  2. What is strongly typed language?

The variable or constant must be defined before it can be used, otherwise it will be an error.

OK, let's get to the bottom, let's introduce the basic types and process control of Swift.

  

  

  3. Basic type

Includes shaping Int, floating-point number Double and float, bool type bool , and String type String. Swift also offers two more powerful basic collection data types,Array and Dictionary. To mention here. In addition to the basic data types, Swift also has a data type called "meta-ancestor", which is unique to him. I'll explain it to you later.

4. Constants and Variables

  

The names of constants and variables are also named by the familiar "hump". Constants point to a value of a specific type, and once assigned, it cannot be modified again. A variable also points to a value of a specific type, but it can be modified as needed. In addition Swift's purpose is concise, so each line can not write semicolons

As I said earlier, Swift is a strongly typed language, so its constants and variables need to be declared before being used. So how do constants and variables be declared?

    Declaration of a constant:

Let begins by stating that you want to declare a constant followed by a variable name, usually named after the hump, with the = sign connected, and then the value you want to assign. If you do not specify an initial value at this point, you need to specify a constant type.

For example: let Usrename = "Tom"//Assign value when declaring

Let UserName String///declaration without initializing value

UserName = "Tom"

Note: Because Swift's syntax requires planning, neither the value nor the operator is left blank, or both are blank spaces.

It is important to note that when declaring a character, a friend who has learned other high-level languages knows that other high-level languages generally enclose the value in single quotation marks when declaring a character, but Swift is different, and its character assigned value or double quotation marks, but is declared with annotations.

For example:

var ch:character = "C"//character value must be in double quotes, and is a character

Constants declared with let start cannot be modified after assignment.

  

   Declaration of a variable:

var begins with the declaration of the other Yu Chang volume, which is not described in detail here, and we refer to the constant declaration.

5. Process Control

  

    

If Esle:

  

For example:

  

Let i = 1

  

If I {

  

Print ("Write Error! ")

}

  

In the case of Swift, this is a wrong formulation. I is a constant, not an expression, and cannot be used as a Boolean type.

The correct writing should be:

  

Let i = 1

  

If I ==1 {

  

Print ("Write correctly! ")

}

  

For: There are two forms.

The first type: for example:

 

  

for var i = 0;i < 1000;i++{

Print ("Say 1000 times I love you!") ")

}

  

  

The second type: for example:

  

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

For a in array {

Print ("\ (a),")//Output Result: 0,1,2,3,4,5,6,7,8,9

}

  

The second method, compared to the first, is most obvious when you don't need to know the length of the array to iterate through each element.

While: is also a loop control statement. Can be transformed perfectly between for and for. For example:

  

  

var i = 0

  

  

While I < 1000 {

  

  Print ("Say 1000 times I love you!") ")

i++

}

Repeat while: loop control statements, as with the Java do while, are executed first, then judge the condition. For example:

  

var i = 0

Repear {

Print ("Say 1000 times I love you!") ")

i++

} While I < 1000

SWITCH: Branch judgment statement. Some of our friends have learned it before, but the role of switch in Swift is stronger and more prominent. It can receive arbitrary constants and variables as long as the front and back types remain consistent. For example:

Let i = "abc"

Switch i{

Case "ABC":

Print ("abc")

Default

Print ("Other cases")

}

Note: Swift requires the switch to be a complete structure, i.e. a switch must have a case above and a default.

Well, today's share is here. Hope can help just learn iOS or want to learn iOS friends, I am also a novice, there is insufficient and wrong place to ask the big God. Let's make progress together! You can follow my Weibo http://www.cnblogs.com/lb0129/

  

  

  

    

  

      

      

      

    

Basic types and Process control for Swift

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.