Member-only story
Everything You Need To Know About Swift Arrays
Arrays are the most common data types. Make sure to learn how to use them in Swift
Arrays are a common data type in Swift. They are like lists where you can store elements (e.g. names or numbers). You are going to need arrays a lot. Thus, it’s good to make sure you learn how to work with them.
In this guide, I will show you everything you need to start working with arrays.
How To Create an Array
Let’s start by creating your first array.
For example, you can create a numbers
array by comma-separating integers inside square brackets like this:
let numbers = [10, 23, 102]
Or a names
array of strings like this:
let names = ["Sofie", "Oliver", "Jennifer"]
Also, you can initialize an empty array to be filled in later on:
var lectureAttendees = []
Count the Elements of an Array
When dealing with arrays, you often want to calculate how many elements there are. In Swift, you can use the count
method of an array to count the number of elements it has.