Everything You Need to Know about Arrays in Swift

What exactly are arrays?

Start here with a quick overview about what arrays are and what they are good for.

Beginner

Arrays are (also) structs in Swift. What does that mean in practice?

It means that arrays are more than just a simple collection of elements. They come out of the box with properties and functions, just like any other struct. Let's learn a little bit about some of these properties and functions.

Beginner

How to create arrays in Swift?

Learn about a few different ways to create arrays in Swift.

Beginner

Creating empty arrays is a little tricky at first, and that is because of Swift's ability to infer type. But when there is not enough context for type inference (such as when we want to create an empty array), we have to do the manual work ourselves.

Once we have a working array, how do we mutate it? We can append elements or entire new arrays, we insert elements, delete elements, or clear the entire array all together.

A quick explanation of four different ways of looping over an array in Swift.

What is the most common mistake when working with arrays in Swift? It is done a lot by beginner programmers and it stems from the fact that arrays in Swift are VALUE-TYPE. What does that even mean? Let's discuss...

Can we put data of mixed type in the same array in Swift? If we simply try it, we get an error, but there is a way. So yes, it can be done. But it doesn't mean it should be done. Let's discuss...

Can we add extensions on arrays? Of course we can, since we already know that arrays are implemented as structs under the hood. They already come out of the box with plenty of properties and functions, and we can add more functionality ourselves by writing extensions, just like we can to any other struct of class object.

This lesson will show how to write generic extensions on arrays, meaning properties or functions that return a generic value type, the same type that the entire array holds, but which can be different for each instance of an array.

Let's learn how to create constrained extensions on arrays. What does that mean? It means additional functionality on arrays (aka extensions), but not for all arrays. Only for some arrays, depending on what type of value they hold.

If we write a .sum computed property, this would not make any sense for arrays of strings, right? It would, however, make sense on arrays of integers. So we will write a computed property called .sum, constrained only to arrays of integers.