Swift array usage

Source: Internet
Author: User

Swift array usage

Swift provides two types of collections: Array and Dictionary. They all share the same type of data, the difference is that the data stored in the array is ordered, and the data stored in the binary dictionary is unordered. The dictionary also has two features: the data stored in the dictionary exists as a key-value pair.

Here we mainly write about the use of arrays in Swift.

I. array type definition

 

To define other variables or constants, if we define a data in Swift, we can also specify its type through the type annotation. In Swift, the array type can be written in two ways:

// Full version: var colorArray: Array
 
  
= [Red, blue, green] // simplified var colorArray1: [String] = [red, blue, green] // you can use derivation to declare the array var colorArray2 = [red, blue, green] var integerArray = [1, 2, 3]/* Note: 1. When defining an array, specify the data type in the array. We must store the array of this type in a unified manner. 2. When we define an array without specifying the data type in the array, the array is of the anyObject type by default. */
 

 

2. When defining an array, we can not specify the array type:

 

var inferList = [eggs,123,true]for item in inferList{    println(inferList contain item : (item))}
It should be noted that the NSArray array is generated in this case.

 

 

Checks whether the array is empty. If the array in Swift is empty, you can directly call the isEmpty method.

If inferList. isEmpty {println (inferList is empty)} else {println (inferList is not empty )}

 

You can also use the number of array elements for determination:

 

If inferList. count = 0 {println (inferList is empty)} else {println (inferList is not empty )}

 

 

3. swift provides two methods to add elements to arrays.

First, add new elements to the array by calling the nested method (append) of the array.

 

inferList.append(hello)

 

Type 2: add new elements to the current array using the addition and value assignment operator

 

InferList + = [Hello]

 

/*

The second method is more powerful: Batch addition can be performed, and arrays added in batches can only store one data type.

InferList + = [black tea, green, black tea]

*/

 

4. Modify array elements

To modify elements in an array in swift, You can first obtain the value of an element in the array by using the array name, square brackets, and the following standard.

For example, obtain the first element of inferList.

 

var firseItem = inferList[0]

 

Modify the value of a specific element

 

inferList[0] = egg

 

Swift provides the ability to modify array elements in batches.

 

InferList [4... 6] = [Haha, ]/* in this case, elements 4 and 5 are modified, and elements 6 and later are removed. If they are not removed, inferList [4... 6] = [Haha, haha, hee] */

 

5. Insert and remove elements from Arrays

Swift inserts a new element at any specified position in the array by calling the insert (atIndex :) method.

 

inferList.insert(insertElements, atIndex: 0)

 

In Swift, the removeAtIndex method is called to remove the specified position element. Other elements are automatically moved forward.

 

inferList.removeAtIndex(0)

 

InsertElements is removed in the first position.

You can also use removeLast () to remove the last element.

 

inferList.removeLast()

 

Last Element Black Tea removed


Vi. array Traversal
There are two methods to traverse arrays in Swift:

 

1. Quickly traverse the array through a for in Loop

for item in inferList {    println(item)}

2. Use the enumerate global function to facilitate the array. This function will return the elements in the array as the metadata of the (index, value) type.

 

for (var (index,value)) in enumerate(inferList) {    println(item : (index), value : (value))}


 




 

 

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.