Better Programming

Advice for programmers.

Follow publication

Member-only story

Everything You Need To Know About Swift Arrays

Artturi Jalli
Better Programming
Published in
5 min readMay 4, 2021
Code on laptop
Photo by Christopher Gower on Unsplash.

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.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Write a response