Different Ways to Create an Array in Swift

Updated: April 17, 2023 By: Khue Post a comment

An array in Swift is a collection of values of the same type that are stored in an ordered list (with that being said, it’s possible to construct an array with mixed types, and you will learn how to do so in the final section of this tutorial). An array can store any kind of element, such as integers, strings, or class instances.

This concise, straight-to-the-point article will walk you through a couple of different ways to create an array in Swift. No more delay; let’s get started.

Using array literal

This is the easiest and most convenient way to create an array. You just write the values you want to store in the array inside square brackets, separated by commas.

Example:

// an array of integers
var numbers = [2, 4, 6, 8]
print(numbers)

// an array of strings
var colors = ["red", "blue", "green"]
print(colors)

// an empty array of integers
var emptyArray: [Int] = []
print(emptyArray)

Output:

[2, 4, 6, 8]
["red", "blue", "green"]
[]

A pragmatic software developer will be very likely to use this approach in more than 99% of his or her work.

Using an Array initializer

Using the Array initializer with a default value and a count

This is a way to create an array that has a fixed number of elements that are all the same. You put the word Array followed by parentheses, and Inside the parentheses, you write the value you want to repeat and how many times you want to repeat it.

Example:

// an array of five red strings
var colors = Array(repeating: "red", count: 5) 
print(colors)

// an array of ten zeros
var zeros = Array(repeating: 0, count: 10)
print(zeros)

Output:

["red", "red", "red", "red", "red"]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

It will be very useful when you need an array whose number of elements is especially large (thousands or millions?).

Creating an empty array

You can initialize an empty array that temporarily has no elements and no positions (new elements can be added later) like this:

// an empty array of integers
var emptyIntegerArray = [Int]()

// an emtpy array of strings
var emptyStringArray = [String]()

If you take a closer look at the code above, you can notice that the word Array is omitted.

Using the Array initializer with an existing array or a sequence

This is a nice technique to create an array that copies the values from another array or a sequence. A sequence is anything that has a series of values, such as a string or a range.

Example:

// an array of strings from another array
var fruits = Array(["apple", "banana", "orange"]) 
print(fruits)

// an array of characters from a string
var letters = Array("abc") 
print(letters)

Output:

["apple", "banana", "orange"]
["a", "b", "c"]

Creating a Swift array with mixed data types

Using protocols

You can use the protocol-oriented approach to create a Swift array with mixed data types. This is type-safe, but it requires you to define the protocol and make the types conform to it.

Example:

// define a protocol that does not have any requirements
protocol MixedValue {}

/* 
make the types that you want to store in the array conform to the MixedValue protocol 
by using empty extensions
*/
extension String: MixedValue {} 
extension Int: MixedValue {} 
extension Bool: MixedValue {}

// create an array of MixedValue type and store different kinds of values in it
var mixedArray: [MixedValue] = ["Hello", 1, true]

// add new elements to the array
mixedArray.append("World")
mixedArray.append(2)
mixedArray.append(false)

print(mixedArray)

Output:

["Hello", 1, true, "World", 2, false]

Using Any type

You can use an array of Any type, which can store any kind of value. However, this is not recommended since you lose the type safety and have to cast the values when you access them.

Example:

// this array contains numbers, strings, and booleans
var messyThings:[Any] = [1, 2, 3, "book", "clothes", true, false]
print(messyThings)

Output:

[1, 2, 3, "book", "clothes", true, false]