Learn Swift-arrays (array)-Continuous updates

Source: Internet
Author: User

collection type--array

Array is a collection type in swift: arrays, which store multiple values of the same type using an ordered list, the biggest difference from the nsarray of OC is thatSwift's array is a value type, and an array of OC is a reference type

methods for declaring arrays
If declaring a mutable array is set to a variable var somemutablearray = [Int] ()  //declares an empty array of type int//the opposite declares an immutable variable set as constant let Somearray = Array (Count:5, Rep eatedvalue:0.03)    //declares an immutable group, the type custom inference is a double type, contains 5 elements, their values are 0.03//the declaration method of the directly set element let Someint = [3,5,8,10,91]// Declares an array that can contain any type var array = [any] ()

accessing an element or other property in an array
Let someint = [3,5,8,10,91]someint.first   //takes the first element of the array, if the array is empty return nilsomeint.last    //Take to the last element of the array, if the array is empty return Nilsomeint.count   //Get the number of elements in the array someint.isempty//Determine if the array is empty return truesomeint[4]//The      value of the 4th element is accessed through the subscript script, Note that the subscript is calculated from 0 let subints = someint[2...4]  //The element is accessed through a range, returning an array

Add a new element to an array
var somemutablearray = [Int] ()  //declares an empty array type of intsomemutablearray.append (5)      //Add an element 5someMutableArray + = [11, 0]     //Splicing An array Somemutablearray.insert (3, atindex:1)  //Insert a value in the specified subscript

Delete Elements of an array
var Somemutablearray = [5, ten, A, a, a, A, 411]somemutablearray.removelast ()   // Delete last element Somemutablearray.removefirst ()  //Delete First element Somemutablearray.removerange (0...2)// Delete a range of elements somemutablearray.removeatindex (0)   //Delete the element at the specified position somemutablearray.removeall ()        //Delete all elements

to modify an element in an array
var someint = [[1, 2, 3]/(+, +, 558]someint[5] = 111            //Modified by subscript script someint[0...2]  

Common traversal methods
var someint = [[+], +, +, +, +, +, 558]for item in Someint {    print (item)//item automatically infers type}for (index, value) in some Int.enumerate () {    //traversal through a tuple, index is subscript, value is    print (index, value)}

Learn Swift-arrays (array)-Continuous updates

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.