Array simple usage
//------------------------------------------------------------------------------ //1. Array Definitions//1> uses [] to quickly define an array, with each array element used, delimited//the data elements in the 2> array can be of different typesvar array = ["Hello","Swift",1,1.2] //------------------------------------------------------------------------------ //2. If you want the data stored in the array to be of the same data type//You can specify the data type in the array when you define it, with the following code:// //1> Array1 is an array that allows only strings to be storedvar array1:[string] Array1= ["Hello","Swift"] //2> Array2 is an array that is allowed to hold only intvar array2:[int] = [1,2,3,4,5] //------------------------------------------------------------------------------ //3. Creating an array with generics//type in <> when using a generic definition array to specify the type of data stored in the array// //1> Array3 is an array that is allowed to hold only int, and () instantiates an arrayvar array3 = array<int>() //2> When instantiating an array, you can use the constructor of the array to specify the initial members of the array//instantiate an array that contains three integers of 20var nums = array<int> (count:3, Repeatedvalue: -) //------------------------------------------------------------------------------ //4. Array Common operations//1> Append elementNums.append ( +) //You can also use operators to add elements directly to an arrayNums + = - //2> inserting elementsNums.insert ( +, Atindex:0) Nums.insert (Ten, Atindex:3) //You can enter the variable/constant name directly in playground to view the current contentNums//3> Delete the specified position elementNums.removeatindex (2) //using variables to record the nums array, essentially making a memory copy, subsequent changes to the array, will not affect the contents of the variable arraysvar resultnums =nums var a= nums[3] Var b= -var c= A +b//4> Delete End elementnums.removelast () nums//5> Delete all elements, but reserve storage spaceNums.removeall (keepcapacity:true) //Displays the contents of the Resultnums, and the contents of the array are not affectedresultnums nums//6> Array Merging//To save an array of the same data type, you can use + direct mergevar Mergearray = array2 + resultnums
RELATED links:
Swift basic usage-arrays Array
Swift basic usage-arrays Array