iOS Tutorial [SWIFT] – What Are Arrays?

In this iOS tutorial SWIFT arrays, we’re going to show you what arrays are and how to use them through some great examples.

Definition

To put it simply, arrays allow you to store objects in a list to use later. In Swift, these objects must be all the same type, however, you can bypass this by creating an array of type “AnyObject”. When you use “AnyObject”, you’re using a protocol which creates an instance of any type of object. It’s pretty self-explanatory, to be honest.

You might be interested in: [SWIFT] Value and Reference Types

Here’s an example of a basic array:

var myArray = [Int]()

If we wanted to be more explicit and tell Swift that we are be going to make an array of type Int we would declare the line above like so:

var myArray:Array = Array()

Arrays and Objects

Earlier we discussed how we could create arrays that store different types of objects, and here is how we would go about doing that:

var myAcceptingArray: Array = Array()
 
myAcceptingArray.append([1,2,3,4,5])
 
myAcceptingArray.append("Hello")
 
print(myAcceptingArray)

The console would return “1,2,3,4,5 Hello”, and would not give us any errors. If you noticed, we used something called “append” in the second and third lines of our code. “Append” pretty much means addObject if you remember that from Objective-C.

Recommended for you: Getting Started With Swift 3.0

Different Variables

What if I told you that there was a much simpler way of declaring an array that could hold many different types of variables by using objects in Swift from Objective-C. Here’s how you do it:

var myArray:NSArray = [1, "hi", Float(4)]

As you can see from above, we put an integer, string, and float into one array. As expected, we received no errors from the console.

If you’re familiar with Objective-C, you’ll know that there are two types of arrays. NSMutableArray and NSArray. The difference between these two is that one is changeable and the other on isn’t. In Swift, we simply use “var” when creating an array to make it mutable, and “let” when we don’t want the content of our array to change.

Inserting and Removing Objects in Arrays

To insert an element at the beginning of an array, you’re going to want to use “insert” and “atIndex”. The first word is adding an object and “atIndex” is telling Swift where to add the object in your array’s sequence. Here’s a quick example:

var numbers = [1,2,3]
 
numbers.insert(0, atIndex:0)

Now if we print “numbers” it will return: 01,2,3. This is because we added the number “0” to the index of 0, which is always the first one in any array.

There are two ways to go about removing items in an array. You can use the removeLast() function to remove the last object in your array, and you can also use “removeAtIndex” to be more precise about which object you want to remove.

var people = ["Henry", "Bob", "Sarah"]
 
people.removeAtIndex(1)

The second line of code will remove “Bob” since he’s at the 1st index. Henry is at 0 and Sarah is at 2.

You might be interested in: The difference between “!” and “?” in Swift

Iterating Over an Array

When you iterate through an array, you’re getting each object from it from beginning to end. Here is an example on how to iterate through a basic array:

var peopleArray = ["Henry", "Bob", "Sarah"]
 
for names in peopleArray
 
{
 
    print(names) //This will return "Henry", "Bob" and "Sarah"
 
}

To get the current index to teach the objects in your array, you can use the “enumerate()” function like so:

for (i, names) in peopleArray.enumerate()
 
{
 
print("The index /(i) has the person /(names)")
 
}

The console will return the index of either 0,1,2 and the names “Henry, Bob and Sarah”.

To create an array that is pre-populated with data, you may use the “repeatedValue” parameter:

var exampleArray = [Int](count: 5, repeatedValue:1) //This will return: 1,1,1,1,1

Lastly, to empty an array you use the “[]”. Here’s a quick example:

myArray = [] //This code will empty myArray

If you liked this iOS tutorial SWIFT arrays, take a look at some of our other stuff at the tutorial section.

 

Leave a Reply

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