The array of Swift uses

Source: Internet
Author: User

Swift provides two types of collections, one of which is an array Array, the other is a dictionary Dictionary, What they have in common is that they are all used to store the same type of data, and the difference is that the data stored in the array is ordered and the data stored in the dictionary is unordered. Dictionaries also have two attributes, that is, the data stored in the dictionary is a key-value pair (key-value) Form.

Here is the main write about the use of array types in Swift

One, the array type definition

Like defining other variables or constants, if we define a data in Swift, you can also specify his type by type callout. In Swift , array types are written in two ways:

One is the full version: var colorarray:array<string> = ["Red", "Blue", "green"]//another is a simplified version of Var colorArray1: [String] = ["Red", "blue "," green "]//can use derivation to declare the array var colorArray2 = [" Red "," Blue "," green "]var Integerarray = [1,2,3]/* Note: 1, when we define an array, we specify the data type in the array, We must store this type of array    2, and when we define an array, we do not specify the data type in the array, the array defaults to the Anyobject type. */

we can not specify the array type when we define the array:

var inferlist = ["Eggs", 123,true]for item in inferlist{    println ("inferlist contain item: \ (Item)")}
It is necessary to point out that this is the case when an array of Nsarray is produced.

Determines whether the array is empty, and the array in Swift is null to call the IsEmpty method directly

If Inferlist.isempty {    println ("Inferlist is Empty")}else{    println ("Inferlist Not Empty")}

You can also use the number of array elements to judge:

If Inferlist.count = = 0 {    println ("Inferlist is Empty")}else{    println ("Inferlist Not Empty")}


third, Swift provides two ways to add elements to an array

the first: by invoking an inline method of an array ( Append ) to add a new element to the array

Inferlist.append ("Hello")

Second: Adding new elements to the current array through the addition assignment operator

Inferlist + = ["Hello"]

/*

The second method is more powerful is the batch can be added, the bulk of the array can only hold one data type

inferlist + = [" black tea "," green ", " Black tea " ]

*/


Iv. Modifying array elements

Swift the elements in a modified array can be preceded by the array name + square brackets + Subscript to get the value of an element in the array

if you get the first element of inferlist

var firseitem = inferlist[0]

Modify the value of a specific element

Inferlist[0] = "egg"

Swift provides the ability to bulk modify array elements

INFERLIST[4...6] = ["hehe", "hehe"]/* in this case, 4, 5 elements are modified, 6 and later elements are removed, if not removed <span class= "S1" style= "font-family:arial, Helvetica, Sans-serif; " >inferlist</span><span class= "S2" style= "font-family:arial, Helvetica, Sans-serif;" >[</span><span class= "S3" style= "Font-family:arial, Helvetica, Sans-serif;" >4</span><span class= "S2" style= "font-family:arial, Helvetica, Sans-serif;" >...</span><span class= "S3" style= "Font-family:arial, Helvetica, Sans-serif;" >6</span><span class= "S2" style= "font-family:arial, Helvetica, Sans-serif;" ] = [</span><span class= "S4" style= "Font-family:arial, Helvetica, Sans-serif;" > "</span><span class=" S5 "style=" Font-family:arial, Helvetica, Sans-serif; " > Hehe </span><span class= "S4" style= "Font-family:arial, Helvetica, Sans-serif;" > "</span><span class=" S2 "style=" font-family:arial, Helvetica, Sans-serif; " </span><span class= "S4" style= "font-family:aRial, Helvetica, Sans-serif; > "</span><span class=" S5 "style=" Font-family:arial, Helvetica, Sans-serif; " > Haha </span><span class= "S4" style= "Font-family:arial, Helvetica, Sans-serif;" > "</span><span class=" S2 "style=" font-family:arial, Helvetica, Sans-serif; " </span><span class= "S4" style= "Font-family:arial, Helvetica, Sans-serif;" > "</span><span class=" S5 "style=" Font-family:arial, Helvetica, Sans-serif; " > Hee </span><span class= "S4" style= "Font-family:arial, Helvetica, Sans-serif;" > "</span><span class=" S2 "style=" font-family:arial, Helvetica, Sans-serif; " >]</span>*/

Five, inserting, removing elements from an array

Swift by calling the The Insert (atindex:) method inserts a new element at any specified position in the array

Inferlist.insert ("Insertelements", atindex:0)

Swift by calling the Removeatindex method removes the specified position element, and the other element automatically moves forward

Inferlist.removeatindex (0)

First Position "Insertelements" be removed

You can also use Removelast () to remove the last element

Inferlist.removelast ()

last element " Black tea " be removed


VI. Array traversal
There are two ways to iterate through an array in swift:

1. Through for in looping through arrays quickly

For item in Inferlist {    println (item)}

2.throughEnumerateglobal function to facilitate an array, this function will use the elements in the array as a type (Index,value) Ganso return

For (Var (index,value)) in enumerate (inferlist) {    println ("Item: \ (index), value: \ (Value)")}







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The array of Swift uses

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.