What is the IOS Swift type array?

There are three collection types in the swift language:

  • Sets
  • Arrays
  • Dictionaries

Generally, a collection type is a way of grouping related items together. These collection types are sometimes referred to as data structures. An array is a data structure that is capable of storing collections of elements, of the same data types or different data types, in an ordered index.

Types of array

There are basically two array types in Swift:

  1. One dimensional array: This is a type of linear array that only uses one subscript definition to specify a particular element of the array. A subscript definition is a way of accessing the element of an array with the element indices. This can be seen as a list that can be vertically or horizontally made.

  2. Multi-dimensional array: This is an array with two or more dimensions, and a subscript definition to specify a particular element of the array. Like tables, this can be viewed in either a vertical or horizontal arrangement. A multi-dimensional is capable of housing array of different arrays.

%0 node_1630032452045 Rice ( 0 ) node_1 Beans ( 1 ) node_2 Yam ( 2 ) node_3 Red OIl ( 3 )
One Dimensional Array : The display diagram shows Item at a particular index with the format : Item ( Index )
%0 node_1630033766129 Wednesday ( 2 ) node_1 Tuesday ( 1 ) node_1630033324824 Monday ( 0 )
Multi Dimensional Array
%0 node_1 Monday ( 0 ) node_2 Beans ( 0 ) node_3 Yam ( 1 )
Beans and Yam have a common index 0 : ( 0, 0 ) = Beans , ( 0, 1 ) = Yam
%0 node_1 Tuesday ( 1 ) node_2 Fried Rice ( 0 ) node_3 Yam Porridge ( 1 )
Fried Rice and Yam Porridge have a common index 1: ( 1, 0 ) = Fried Rice , ( 1, 1 ) = Yam Porridge
%0 node_1 Wednesday ( 2 ) node_2 Beans ( 0 ) node_3 Shawarma ( 1 )
Beans and Shawarma have a common index 2: ( 2, 0 ) = Beans , ( 2, 1 ) = Shawarma

Ways to create arrays in Swift

Arrays can be of type:

  • Int: This hold only integers ( 0, 1, 2 …)
  • String : This hold characters (“welcome”), which are in double quote
  • Any: This can hold any datatypeex: tring, Double, and so on, as the name implies
  • Double/ Float: They are both used to hold decimal numbers23.45, 456.23, but double is more accurate.
// empty array of name
var name: [String]
// array of names has been assign
//different names
// only string is here
name = ["kemi", "shayo", "Tola"]
// empty array of numbers
var numbers: [Int]
// array of numbers has been assign
//different numbers
numbers = [3,4, 6, 7]
// line comment symbol
// string and integers are present here
var mixed: [Any] = [3, "james", 45]

Ways to access an element in an array

This is a way of making modification or fetching information from the elements in an array. two slashes in the code shows comment to explain the code.

// empty array of name
var names: [String]
// array of names is contain three names
names = ["kemi", "shayo", "Tola"]
// print the element in the array of names
for element in names {
print(element)
}

How to modify the elements in an array

Imagine that changes need to be mad, this is simple term will modify an element in an array.

// array of names containing
//Shayo, Christopher and Mathias
var names = ["Shayo", "Christopher", "Mathias"]
// append method add new name to the list of names
names.append("Tola")
print(names)
// let's remove Shayo at index 0
print("This is the name remove at index 0: \(names.remove(at: 0))")

Manipulating elements in an array

When an operation is perform in an array in this regard, the array of numbers is summed up. To see this, take a look at the code snippet below.

//array of numbers having 4, 5, 6, 7
var numbers = [ 4, 5, 6, 7]
// the sum of the numbers is save in a variable sum
// add elements in numbers array
let sum = numbers.reduce(0, +)
// print the sum
print("the sum of numbers = \(sum)") // 22

The importance of an array in a data structure can not be over-emphasized. However, here are various methods used to solving problems in programming, some of which have been used here. I encourage you to practice other methods as well.

Free Resources