Saturday, June 8, 2019

Arrays in Swift, iOS


The simplest type of array is the linear array, which also know as one dimensional array. In Swift, arrays are a zero 
based index. One dimensional array can be written simply as ai where i is index between 0 and n.













Another form of array is the multidimensional array which is a typical matrix

There are three forms of syntax for declaring array in Swift.


















If you do not want to define any values at the time of declaration, the following code should be used.


To declare a multidimensional array use nesting pairs of square brackets.


Retrieve , Add and Remove elements
There are multiple ways to retrieve values from array.  We can retrieve using index or loop through using for…in 
syntax.



Output:







We can iterate through the elements in an array.



Output:











Addin elements to array
There are two ways of adding element to array. Append function can be used to add an element at the end of the 
array and Insert function can be used to insert an element at a specific index in an existing array.


Output:



Using Insert



Output:



Removing elements from array
Similarly, there are two ways of removing elements from an array. By using removeLast() function an element at the
end of array can be removed or remove(at:) function can be used to remove an element at a specific index.



























No comments:

Post a Comment