iOS

iOS Lesson 08 – Arrays in Swift

So with this knowledge bit, we are going to have a deep dive into the foundational data structure that is Arrays in swift.

Open up your Xcode and create a new playground. We are going to call this one “ArraysAreGreat” and delete the Hello world line.

Arrays in Swift:

An array is an extremely important foundational data structure in swift. It is used to store data in a sort of collection of different/similar type

  1. Q) Why do we need arrays?
  2. A) Let’s say that we are running a school, and we want to create an app that holds all of the student’s record, let’s say for this example we just want to store student’s name and a number of their particular course. So, without arrays, you would probably the code something like this. Creating multiple variables for various students.

IOS Lesson: Arrays in Swift

 

We just have 4 students and we started to write some really bad code (creating a variable for each student and saving his/her marks)

Details:

We created 4 variables, and every variable of them has its values. What if we want to do some grading in the marks for each student? With current code structure, we will have to separately write every variable and give its value. Maybe it’s better to structure this in a slightly better way. We can do that with something called an array.

To declare an array we can use the constant ”let” or a variable ”var”. We are going to use the var keyword because we are planning to change the values later.

 

Code:

           var studentMarks = [10.0,50.0,15.0,23.0,45,50.0]

we have created a variable using the var keyword and named it as studentMarks we used the equal sign and then we gave it few values. We separated those values using the comma. This type of array will contain values of any type but if we declare its type explicitly then it will be forced to save a specific type.

Example:

            var studentmarks : [Double] = [10.0,50.0,15.0,23.0,45,50.0]

Now as we have explicitly defined its type so if we would add something to our array that is not double then we are going to get an error.

IOS lesson: Arrays in Swift Code

remove the string entry ”Geeks”.

We can use .append() method to add an entry at the end of the array. In this example we have used print() method to print the total number of elements inside the array then we have used .append() method to add a new entry then again we have used print() method to get the total number of entries inside the array. Even if we would try to add some item that is not of the type that we have defined explicitly it will throw an error.

Arrays in Swift Example

We can also use different syntax to define an array of type double.

Example:

         var studentmarks : Array<Double> = [10.0,50.0,15.0,23.0,45,50.0]

what if we want to remove an element from the array, specifically the third entry”15.0” from the array so what we can do is we can use .remove() method delete an entry at a specified index. So if we want to remove the third entry we will write the code something like this.

Code:

            studentmarks.remove(at: 2)

you would probably be confused after seeing 2 in the arguments. The only explanation of giving 2 is that the index of the array is zero-based. They start from zero instead of one. So the first entry of the array will always be zero and then they move on. After removing the element from the second index if we use print() method to show the number of elements inside the array we will get the updated number of elements that is 6.

In a Nutshell:

you can add something to an array, you can remove things from an array and array should be used whenever you want to use a collection of something to be together. Even though there are few more types that are used for collections but we are going to look at them in the later knowledge bits.

Practice:

Let’s try doing some practice with the different syntax of the array.

Code:

           var studentNames = [String]()       

in this syntax what we have done is that we have created an array of type string and initialized it with o entries. Currently, this array doesn’t have any values inside itself.

Code:

           print(studentNames.count       

if we use the print method we can see that we will get 0.

Arrays in Swift

now, the reason why we want to use an array is that we can loop through the entire array collection with “Loops” and we will look at loops in the next knowledge bit.

Conclusion:

So with this knowledge bit, we have covered arrays. You are going to use these a lot when you are making collections and other similar groups we have seen how we can use them efficiently in the code to make our lives easier. You can get all the code from here.

Please Like, share and subscribe to more!

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular

To Top