Welcome to Swift (initial translation and annotations of Apple's official Swift document 21)---140~147 page (chapter III-collection type)

Source: Internet
Author: User

    Chapter III

Collection Types (collection type)

In Swift, two collection types are provided to store a set of values: arrays and dictionaries. The array stores the same type of values in an orderly manner; The dictionary stores values of the same type that are unordered. A dictionary can be queried and accessed through a unique identifier (that is, the key).

In swift, arrays and dictionaries always have a clear indication of the type of data they store. This means that the wrong type cannot be inserted into an array or dictionary. It also means that you have a clear understanding of the type of data in the array or dictionary that you want to traverse. In Swift, The collection is explicitly declared to ensure that the type of data it can handle is clearly known in development.

  Note the point:

In Swift, arrays (array) are assigned values or are passed to a function or method and are not associated with other types.

Arrays (Array)

An array can sequentially store a set of values of the same type. The same value can occur multiple times in different positions in the array.

In Swift, an array is a value that stores a specified type. Unlike the Nsarray and Nsmutablearray classes in OC, they can store different types of values in OC. In swift, the type of values stored in the array must be explicit. If you declare an array of int, You cannot store values other than int types. In swift, an array is type-safe, and you want to explicitly declare the type of value it stores.

Array type shorthand Syntax (array types abbreviation syntax)

In Swift, the complete notation of an array is: ARRAY<SOMETYPE>, here the SomeType is the type that the array can store. Arrays can also be abbreviated: sometype[]. Although the two forms are functionally identical, we push Use abbreviations. Abbreviations are used in this manual.

Array literals (text array)

A text array is a column of values separated by commas (,) in brackets [], and can be initialized using text arrays:

[Value 1, Value 2, value 3]

In the following code example, an array called Shoppinglist is created to store a string (string) type of value:

var shoppinglist:string[] = ["Eggs", "Milk"]

Shoppinglist is initialized with two string elements

by string[], the variable shoppinglist is defined as an array of type String. Therefore, the array can only store values of type striing (String). Here, the array shoppinglist through two string values ("Eggs" and " Milk ") to initialize.

  Note the point:

The shoppinglist array is defined as a variable (using the var keyword) instead of a constant, because you want to add more elements to the array in the following code example.

In this example, the text array contains two string (string)-type values. This is exactly the same as the type of the variable shoppinglist. Therefore, it is allowed to initialize the array as an array by using two string elements to assign values.

Because of the type prediction mechanism in swift, it is not necessary to specify the type of the array when initializing the text array with the value of the text content. Therefore, the initialization of Shoppinglist can be abbreviated as:

var shoppinglist = ["Eggs", "Milk"]

Because all values in the array are of the same type, Swift can determine that the correct type of variable shoppinglist is string[]

Accessing and modifying an array (access and modification of arrays)

Using the methods and properties of numeric values, you can access and modify an array, or use an array subscript.

View the number of elements of an array, using its read-only property: Count

println ("The shopping list contains \ (shoppinglist.count) items.")

Prints "The shopping list contains 2 items."

Using the Boolean Type property, IsEmpty can detect whether the value of the array's Count property equals 0:

If Shoppinglist.isempty {

println ("The shopping list is empty.")

} else {

println ("The shopping list is not empty.")

}

Prints "The shopping list is not empty."

Using the Append method of the array, you can add a new element to the last face of the array:

Shoppinglist.append ("flour")

Shoppinglist now contains 3 items, and someone is making pancakes "

Alternatively, you can use the compound assignment operator (+ =) to add a new element to the array at the end of the array:

Shoppinglist + = "Baking Powder"

Shoppinglist now contains 4 items

Of course, you can also add an array of the same element type using the compound assignment operator (+ =):

Shoppinglist + = ["Chocolate Spread", "Cheese", "Butter"]

Shoppinglist now contains 7 items

Extract a value from the array, using subscript: in brackets [] after the array name, write the index of the value you want to extract

var FirstItem = shoppinglist[0]

FirstItem is equal to "Eggs"

Notice that the index of the first element of the array is 0 instead of 1. In Swift, all arrays start with 0.

You can change the element value of the corresponding index in the array by subscript:

Shoppinglist[0] = "Six eggs"

The first item in the list was now equal to "Six eggs" rather than "eggs"

Using subscript can also change a range of values in an array, or even replace a set of values, even if the length of the replacement is not the same as the range you specified. The following code example replaces "Chocolate Spread", "Cheese", and "Butter" with "Bananas" and "Apples" :

SHOPPINGLIST[4...6] = ["Bananas", "Apples"]

Shoppinglist now contains 6 items "

  Note the point:

You cannot add a new element to an array by using the Subscript method class. If you use an array subscript that is outside the bounds of the array, a run error will be triggered. You can actually check whether the subscript index is legal by comparing the array's Count property before using subscript. Unless the count value of the array is 0 ( This means that the arrays are empty, otherwise the index of the array with the largest indexes is always count-1, since the index of the array starts at 0.

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.