Home>

A swift array is used to store a sequential list of values ​​of the same type.swift is strictly checked,It does not allow the wrong type to be stored in the array by mistake.

If assignment creates an array to a variable,It is always mutable,This means it can be changed by adding elements, Delete or change its items,But if you assign an array constant to the array,The array is immutable, In other words, its size and content cannot be changed.

Create arrayYou can use the following initializer syntax to create some type of empty array:

var somearray=[sometype] ()

Here's how to create a given size,And use the syntax of an array of initial values:

var somearray=[sometype] (count:numbeofelements, repeatedvalue:initialvalue)

Below is an example,To create an empty int array with 3 elements and an initial value of zero:

var someints=[int] (count:3, repeatedvalue:0)

Here is an array that creates three elements,An example of specifying an array of three values:

var someints:[int]=[10, 20, 30]

Access arrayThe corresponding value can be retrieved from the array using subscript syntax,Pass the value corresponding to the index in parentheses after the array name,as follows:

var somevar=somearray [index]

Here, the index starts at 0, which means that the first element can be accessed using index 0,The second element can be accessed by using index 1,Others are similar.Let's take a look at the creation below,Examples of initializing and accessing arrays:

import cocoa
</p>
<p>
var someints=[int] (count:3, repeatedvalue:10)
</p>
<p>
var somevar=someints [0]
</p>
<p>
println ("value of first element is \ (somevar)")
println ("value of second element is \ (someints [1])")
println ("value of third element is \ (someints [2])")

When the above code is compiled and executed,It produced the following results:

value of first element is 10
value of second element is 10
value of third element is 10

Modify arrayYou can use the append () method or the addition assignment operator (+ =) to add new items to the end of the array,First create an empty array here,Then add new elements to the array,As follows:

import cocoa
</p>
<p>
var someints=[int] ()
</p>
<p>
someints.append (20)
someints.append (30)
someints +=[40]
</p>
<p>
var somevar=someints [0]
</p>
<p>
println ("value of first element is \ (somevar)")
println ("value of second element is \ (someints [1])")
println ("value of third element is \ (someints [2])")

When the above code is compiled and executed,It produced the following results:

value of first element is 20
value of second element is 30
value of third element is 40

You can modify an existing element of an array by assigning a new value at a given index,As in the following example:

import cocoa
</p>
<p>
var someints=[int] ()
</p>
<p>
someints.append (20)
someints.append (30)
someints +=[40]
</p>
<p>
//modify last element
someints [2]=50
</p>
<p>
var somevar=someints [0]
</p>
<p>
println ("value of first element is \ (somevar)")
println ("value of second element is \ (someints [1])")
println ("value of third element is \ (someints [2])")

When the above code is compiled and executed,It produced the following results:

value of first element is 20
value of second element is 30
value of third element is 50

Iterating/traversing an arrayYou can use a for-in loop to iterate through the series.In the example below is the entire set value of the array,As shown below:

import cocoa
</p>
<p>
var somestrs=[string] ()
</p>
<p>
somestrs.append ("apple")
somestrs.append ("amazon")
somestrs +=["google"]
</p>
<p>
for item in somestrs {
 println (item)
}

When the above code is compiled and executed,It produced the following results:

apple
amazon
google

You can also use the enumerate () function, as shown in the following example,It returns the index and the corresponding value:

import cocoa
</p>
<p>
var somestrs=[string] ()
</p>
<p>
somestrs.append ("apple")
somestrs.append ("amazon")
somestrs +=["google"]
</p>
<p>
for (index, item) in enumerate (somestrs) {
 println ("value at index=\ (index) is \ (item)")
}

When the above code is compiled and executed,It produced the following results:

value at index=0 is apple
value at index=1 is amazon
value at index=2 is google

Adding two arraysUse the addition operator (+) to add an array of the same type,This will produce a new array that is an array from the addition and combination of two array values,as follows:

import cocoa
</p>
<p>
var intsa=[int] (count:2, repeatedvalue:2)
var intsb=[int] (count:3, repeatedvalue:1)
</p>
<p>
var intsc=intsa + intsb
</p>
<p>
for item in intsc {
 println (item)
}

When the above code is compiled and executed,It produced the following results:

2
2
1
1
1

count attributeYou can use read-only count (count) array attributes.Find out the number of elements in the array shown below:

import cocoa
</p>
<p>
var intsa=[int] (count:2, repeatedvalue:2)
var intsb=[int] (count:3, repeatedvalue:1)
</p>
<p>
var intsc=intsa + intsb
</p>
<p>
println ("total items in intsa=\ (intsa.count)")
println ("total items in intsb=\ (intsb.count)")
println ("total items in intsc=\ (intsc.count)")

When the above code is compiled and executed,It produced the following results:

total items in intsa=2
total items in intsb=3
total items in intsc=5

Empty attributeUse the isempty property of a read-only array to find out if an array is empty.As shown below:

import cocoa
</p>
<p>
var intsa=[int] (count:2, repeatedvalue:2)
var intsb=[int] (count:3, repeatedvalue:1)
var intsc=[int] ()
</p>
<p>
println ("intsa.isempty=\ (intsa.isempty)")
println ("intsb.isempty=\ (intsb.isempty)")
println ("intsc.isempty=\ (intsc.isempty)")

When the above code is compiled and executed,It produced the following results:

intsa.isempty=false
intsb.isempty=false
intsc.isempty=true
  • Previous thinkphp how to get client IP
  • Next Very simple Ajax request example with source code