[Swift first sight] Swift array and swift Array

Source: Internet
Author: User

[Swift first sight] Swift array and swift Array
In Objective-C, arrays are common data types. In Swift, there are NSArray and NSMutableArray in OC, however, in Swift, only let and var are used to determine whether the array is variable. arrays in Swift are of type security. Therefore, the type must be clear before a data is saved to an array, if we create a String-type array, we cannot add non-String data types to the array, which is an important difference between Swift and OC.

Array Construction
Let's take the example of creating an array and storing the Int type array:

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]

Array quantity: the array has a read-only attribute count to obtain the number of data items in the array.
array.count

Check whether the array is empty. You can use isEmpty to check whether count is 0.
if array.isEmpty {    println("array is Empty")}else{    println("array is not Empty")}

You can use the append method to add new data items after the array:
array .append(6)

In this case, the array has five values,
When the data type of the array is string, you can also use the addition assignment operator (+ =) to directly add new data items to the end of the array;
Addition operators can also directly add arrays of the same type:
array += [7,8]

When retrieving data items in an array, you can use indexes to obtain values:
var intV = array[0]

Note: array indexes, whether OC or Swift, start from 0.

When modifying an item in an array, you can also change it through indexes:
array[0] = 9

In this case, the array is: [9, 3, 4, 5, 6, 7, 8]
Swift can also change multiple data values at a time by Subscript:
array[1...3] = [10,11,12]

The array value is: [9, 10, 11, 12, 6, 7, 8].
When a new item is added at the end of the array, the subscript cannot be used to add the new item. If the array is out of bounds, a runtime error is thrown.

Add a data item before a specific index value and call the insert (atIndex) of the array to add it:
array.insert(13, atIndex: 0)

The array value is: [13, 9, 10, 11, 12, 6, 7, 8]

Similarly, the removeAtIndex method is used to remove an array;
Use removeLast to remove the last item.

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


Swift provides an enumerate function to traverse arrays and return both data items and index values:
for (index, value) in enumerate(array){    println("index : \(index) value: \(value)")}


The printed value is:
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 with a specific size and all data is defaulted, swift provides an array constructor:
var newArray = [String](count: 4, repeatedValue: "test")

Of course, swift has the type inference function, which can be defined as follows:
var newArray2 = Array(count: 3, repeatedValue: "today")

We know that we can combine two arrays with the same data type to form a new array:
var newArray3 = newArray + newArray2


Everyone is welcome to study and guide.

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.