Swift Arrays

  Total Views : 380
  Comments : 0
  Keywords : Swift arrays are used to store values of same type of elements in ordered list, Swift store same value in arrays multiple times in different positions with example

Here we will learn arrays in swift with examples and how to use swift arrays to store same type of multiple values in one variable, creating an arrays, accessing elements in arrays, add or delete elements in arrays, update elements in arrays and iterate over items in an arrays with examples.

Swift Arrays

In swift arrays are used to store same data type of multiple values in ordered manner. In array we can store same value in multiple times at different positions based on our requirements.

 

In swift arrays we can store any kind of elements like either strings or integers or other element types based on our requirements.

 

We can use arrays in swift programming language whenever we want to hold multiple values of single data type in one variable.

 

Following is the pictorial representation of arrays in swift programming language.

 

Swift Array Collection Types with Examples

 

If you observe above image we are storing values in swift arrays based on index positions in ordered manner.

 

By using index we can access arrays elements and perform update or delete operations on array elements based on our requirements in swift programming language.

 

In swift we can create arrays by surround a list of comma separated values with square brackets.

Arrays Declaration

Following are the different ways of defining or declaring arrays in swift programming language.

 

Simple way of defining an array elements with comma separated values surrounded by square brackets.

 

// An array of integer elements

var numbers = [1, 2, 3, 4]

 

// An array of string elements

var names = ["Suresh", "Rohini", "Praveen", "Tulasi", "Sateesh"]

Following is another way of create an array with data type in an array declaration.

 

var nums: Array<Int> = [1,2,3]

print(nums)

// It prints [1, 2, 3]

Following are the different ways of create an empty arrays with data type in an array declaration.

 

var intvals = Array<Int>()

var intarr = [Int]()

If we want to define an array with fixed number of default values then by using following approach we can define default values.

 

var numofvals = [Int](count: 10, repeatedValue: 1)

print(numofvals)

// It prints [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

When we run above code snippet in swift playground we will get [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] result.

 

Now we will see how to access array elemetns in swift programming language with examples.

Accessing Array Elements

In swift we can access array elements by using index positions. Generally in arrays index position will start from zero (0).

 

Following is the example of accessing an array elements using index positions.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

print(names)

print(names[0])

print(names[1])

print(names[2])

print(names[3])

When we run above example we will get result like as shown below.

 

[suresh, rohini, praveen, sateesh]

suresh

rohini

praveen

sateesh

We can access an array elements by using for-in loop in swift programming language. Following is the example of accessing array elements using for-in loop.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

for name in names {

print(name)

}

When we run above example we will get result like as shown below

 

suresh

rohini

praveen

sateesh

In swift we have properties called first and last to access first and last elements of an array elements.

 

Following is the example of accessing first & last elements of an array.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

var firstname = names [0]

print(firstname)

print(names.first, names.last)

If you observe above example first we element we can access either by using index position “0” or “first” property. When we run above example we will get result like as shown below.

 

suresh

suresh, sateesh

This is how we can access elements from an array based on our requirements in swift programming language.

Check Array Empty or Not

By using isEmpty property we can check whether the given array contains elements or not in swift programming language.

 

Following is the example of using isEmpty property to check whether the given array contains elements or not.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

if names.isEmpty  {

print("Array is Empty")

} else {

print("Array Contains Elements")

}

If you observe above example we are checking an array contains elements or not using isEmpty property.

 

When we run above program in swift playground that will return result like as shown below

 

Array Contains Elements

This is how we can check whether the given array contains elements or not in swift programming language based on our requirements.

Get Array Elements Count

In swift by using count property we can get the count or number of elements exists in an array.

 

Following is the example of using count property to get number of elements exists in an array.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

if names.count > 0 {

print("Array Contains \(names.count) Elements")

} else {

print("No Elements")

}

If you observe above example we are checking whether given array contains elements or not using count property.

 

When we run above program in swift playground that will return result like as shown below

 

Array Contains 4 Elements

This is how we can get number of elements exists in an array in swift programming language based on our requirements.

Add Elements to an Array

In swift we can add elements to an array by using built-in append() method or += operator based on our requirements.

 

Following is the example of adding an elements to an existing array in swift programming language.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

names.append("sudheer")

names += ["madhav"]

print(names)

If you observe above example we are adding new elements to an “names” array using append() method and += operator.

 

When we run above program in swift playground that will return result like as shown below

 

[suresh, rohini, praveen, sateesh, sudheer, madhav]

Suppose if we want to insert elements at particular index position of an array then by using insert() method we can insert elements in an array based on our requirements.

 

Following is the example of inserting new elements at particular position in an array using insert() method in swift programming language.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

names.insert("madhav", atIndex: 2)

print(names)

If you observe above example we are inserting new element at the index position of “3” using insert() method.

 

When we run above program in swift playground that will return result like as shown below

 

[suresh, rohini, madhav, praveen, sateesh]

This is how we can insert or append elements to an existing array in swift programming language based on our requirements.

Remove Array Elements

In Swift, we can easily delete or remove the elements from an array by using removeLast() and removeAtIndex() methods based on our requirements.

 

Following is the example of removing last element of an array by using removeLast methods.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

names.removeLast()

print(names)

If you observe above example we are deleting last element of an array by using “removeLast” method.

 

When we run above example we will get result like as shown below

 

[suresh, rohini, praveen]

Now we will see how to delete array elements based on index position by using removeAtIndex method.

 

Following is the example of deleting an array elements using removeAtIndex method.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

names.removeAtIndex(2)

print(names)

If you observe above example we are deleting array element at the index position of 2 by using “removeAtIndex” method.

 

When we run above example we will get result like as shown below

 

[suresh, rohini, sateesh]

This is how we can delete elements in an array based on our requirements in swift programming language.

Update an Array Elements

In swift we can easily update an array elements by assigning new values based on index positions.

 

Following is the example of updating an array element at particular index position in swift programming language.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

names[2] = "madhav"

print(names)

If you observe above example we are updating an array element at the index position of 2.

 

When we run above example we will get result like as shown below

 

[suresh, rohini, madhav, sateesh]

This is how we can update an array elements in swift programming language based on our requirements.

Iterating Over an Array Elements

By using for-in loop we can iterate or loop through an elements in array based on our requirements in swift programming language.

 

Following is the example of iterating over an elements in array by using for-in loop.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

for name in names {

print(name)

}

When we run above example we will get result like as shown below

 

suresh

rohini

praveen

sateesh

Following is another example of accessing an array elements using for-in loop with numeric bullet for each item.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

for (index, name) in names.enumerate() {

print("\(index + 1). \(name)")

}

When we run above example we will get result like as shown below

 

1.suresh

2.rohini

3.praveen

4.sateesh

This is how we can loop through or iterate over an array elements in swift programming language based on our requirements. 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User