Welcome to Swift (initial translation and annotations of Apple's official Swift document 24)---163~170 page (end of chapter III-fourth chapter flow control)

Source: Internet
Author: User
Tags case statement switch case

Mutability of Collections (variable collection)

Dictionaries and Arrays store multiple values in a collection. If you create an array or dictionary and assign a value to a variable, the set is mutable (mutable). This means that after you create the collection, you can still change the size of the collection, add elements to the collection, or delete existing elements. instead , if you create an array or dictionary assigned to a constant, then this set is not modifiable, meaning that the dictionary or array is immutable (immutable)

For a dictionary, immutable means that you cannot replace an existing key-value pair, an immutable dictionary that cannot be changed when it is created.

For arrays, immutable is slightly different from the dictionary, although it is still not allowed to modify the size of the array, but it allows you to reset the new value to the value that already exists. This makes the array type size fixed in Swift still has a high execution efficiency.

The variability of an array is still valid for how to modify or assign an array instance.

Fourth Chapter

Control flow (Process Control)

Swift provides a process control structure similar to the C language. These include for and while loops, if and switch statements execute different branch code, and like break and continue statements to implement jumps in code.

In addition to the traditional for loop, Swift adds a for-in loop, which makes it easier to iterate through arrays, dictionaries, scopes, and other sequences.

Swift's switch statement is also more powerful and precise than the C language. In swift, each case statement of switch is no longer dropped into the next one, which avoids the error caused by the absence of a break in the C language. It can match many different pattern types, Includes a range match, a tuple, or a specified type. The matching value in a switch case can be bound to a temporary constant or variable in the case body, and the where branch statement can be used for complex match conditions in each case.

For Loops (for Loop)

A For loop is the execution of a set of statements that repeats some number of times. Two for loops are available in Swift:

      • For-in executes each element in a range, sequence, and collection of statements.
      • For-condition-increment executes a set of statements that know the specified conditions are met. Typically, use increments after each loop.

For-in

You can use the for-in loop to iterate through the elements of the entire collection, such as the elements of the array, the characters in the string, and the values of the range.

The following code example outputs five lists:

For index in 1...5 {

println ("\ (index) times 5 is \ (Index * 5)")

}

1 times 5 is 5

2 times 5 is 10

3 times 5 is 15

4 times 5 is 20

5 times 5 is 25

The elements that traverse the collection are bands that use a closed range of 1 to 5. Therefore, use the Closed range operator (...). The value of index is set at the first time by 1 in the bit range, and then the statement inside the loop is executed. In this example, there is only one statement in the loop that outputs the current value of index in each loop. After the statement executes, index is updated to 2 in the range, and then the PRINTLN function is called again. This process until the condition reaches the last value of the range.

In the preceding code example, index is a constant that automatically sets the value in the range each time the loop is traversed. Therefore, it does not have a declaration definition before it is used. It is implicitly defined in the loop, so it is not necessary to use the LET keyword.

  Note the point:

The existence of a constant index is limited to within the loop. If you check the value of index after the loop is complete, or if you need to make it a variable instead of a constant, you must define it yourself before the loop.

If you don't need each value in the range, you can use an underscore (_) instead of a variable:

Let base = 3

Let power = 10

var answer = 1

For _ in 1...power {

Answer *= Base

}

println ("\ (base) to the power of \ (power) is \ (answer)")

Prints "3 to the power of 59049"

This code example calculates the value of a number of square values (in this case, 3 of the 10-time Square).

Use the for-in loop in the array to iterate through the elements:

Let names = ["Anna", "Alex", "Brian", "Jack"]

For name in Names {

println ("Hello, \ (name)!")

}

Hello, anna!.

Hello, alex!.

Hello, brian!.

Hello, jack!.

You can also traverse the dictionary to access its key-value pairs, and during traversal, each key-value pair in the dictionary is returned as a (key, value) tuple, and you can use the named constants in the loop to resolve the members in the (key, value) tuple. In the following example, The dictionary key is parsed into the constant animalname, and the value of the dictionary is parsed into the constant legcount:

Let Numberoflegs = ["Spider": 8, "Ant": 6, "Cat": 4]

For (Animalname, Legcount) in Numberoflegs {

println ("\ (animalname) s has \ (legcount) legs")

}

Spiders have 8 legs

Ants have 6 legs

Cats have 4 legs

Elements in a dictionary do not need to be traversed in the order in which they were written. The contents of the dictionary are not sequential, and the order of all the elements traversed is not guaranteed to be obtained in order.

In addition to dictionaries and arrays, you can use for-in to set each character in a variable string:

For character in "Hello" {

println (character)

}

H

E

L

L

O

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.