Swift Collection types

Source: Internet
Author: User



The swift language provides both array and dictionary collection types, which are used to store the same data type sequentially, and the dictionary is a key-value pair that stores data of the same type. In Swift, data and collection stores must have a clear data type that can only access the specified data type.


Array


Arrays are ordered collections that store data of the same type, and the same value can appear multiple times in an array.


Declaring an array
var arraylist:stting[] = ['zampo','is','best  ']

The ArrayList variable is declared as an array of type string, marked with shring[], so that only data of the string type can be stored.
Since Swift will automatically determine the type, we do not need to define the array type clearly, ArrayList can write:

var arrayList = ['zampo','is','best' ]
 Accessing and modifying arrays 


Gets the number of data items in the array:


println ("The array list contains \ (arrayList.count) items")
// Output: The array list contains 3 items


Determine if the array is empty:


 
if arrayList.isEmpty{
      println("The array list is empty")  
}else{
      println("The array list is not empty")       
}


To add new data to the array after using append:


arrayList.append ("hey");
// The fourth piece of data!
arrayList + = "eady"
// use (+ =) to add data after the array
arrayList + = ["not", "the", "best"]


Use subscript to get the data:


var FirstItem = arraylist[0]


Use subscript to modify the data:


arraylist[0"Swift" 
arraylist[3...5["per", "happy"]//Replace subscript is 3 and 5, delete between 3 and 5


Call the array's insert (Atindex:) method to add a data item before a specific index value:


Arraylist.insert ("test", Atindex:2)// at this point, test becomes the third item of the array 


To move the value of an item in an array:


var test = arrayList.removeAtIndex (2)
// At this time, the index of 2 is removed, and the method returns the value of the removed index. At this time, the data will automatically fill the index.


Move the first element of the divisor group, the last element:


arrayList.removeAtfirst ()
// delete the first item
arrayList.removeAtLast ()
// Remove the end so that we don't need to get the number of arrays by counting 





Swift Collection types


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.