Swift's initial "Swift" array

Source: Internet
Author: User

Arrays are common data types in Objective-c, as is the case in Swift, where there are Nsarray and Nsmutablearray in OC, but only let and Var are used in Swift to differentiate whether an array is mutable, Arrays in Swift are type-safe, so the type must be clear before a data is deposited into an array, and if we create an array of type string, then the data type of non-string cannot be added in the array, which is a very important difference between swift and OC.

Construction of Arrays
Let's take an example of creating an array and storing an array of type int:
var array = [2, 3, 4, 5]var array1:array = [2, 3, 4, 5]var array2:array<int> = [2, 3, 4, 5]var array3: [Int] = [2, 3, 4, 5]

Number of arrays: The array has a read-only property count to get the number of data items in the array.
Array.count

Check if the array is empty you can use IsEmpty to check if Count is 0
If Array.isEmpty {    println ("Array is empty")}else{    println ("Array is not Empty")}

When you want to add a new data item after the array, you can use the Append method to add:
Array. Append (6)

In this case, there are 5 values in the array.
When the data type of an array is a string, you can also use the addition assignment operator (+ =) to add new data items directly after the array;
The addition operator can also add an array that has the same type directly:
Array + = [7,8]

When you get the data item in the array, you can use the index to get the value:
var Intv = array[0]

Note: For both OC and Swift, the index of the array starts at 0.

When you modify an item in an array, you can also change it by index:
Array[0] = 9

At this point the array is: [9, 3, 4, 5, 6, 7, 8]
You can also change multiple data values at once by using the subscript in Swift:
ARRAY[1...3] = [10,11,12]

At this point the array values are: [9, 10, 11, 12, 6, 7, 8]
When you add a new item at the end of the array, you cannot use the subscript to add a new item, and when the array is out of bounds, a run-time error is raised.

To add a data item before a specific index value, call the array's insert (Atindex) to add:
Array.insert (atindex:0)

The array values at this time are: [13, 9, 10, 11, 12, 6, 7, 8]

Similarly, the Removeatindex method is used to remove an item of the divisor group;
Use Removelast when removing the last item

Array Traversal
In general, we use the for-in loop to iterate through all the data items in the array.
For I in array {    println (i)}


Swift provides a enumerate function to iterate through an array, returning both the data item and the index value:
For (index, value) in enumerate (array) {    println ("index: \ (index) value: \ (Value)")}


The values that are printed at this time are:
index:0 value:13
Index:1 Value:9
Index:2 value:10
Index:3 value:11
Index:4 Value:12
Index:5 Value:6
Index:6 Value:7
Index:7 Value:8

If we need to create an array that has a specific size and all of the data is default, Swift provides an array constructor:
var newarray = [String] (Count:4, Repeatedvalue: "Test")

Of course, Swift has type inference capabilities, and we can also define the following:
var newArray2 = Array (Count:3, Repeatedvalue: "Today")

We know that two arrays of the same type of data items we can combine (+) to form a new array:
var newArray3 = NewArray + newArray2


You are welcome to study and guide together.

Swift's initial "Swift" array

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.