How to create an empty array in Swift?


In Swift, there are several ways to create an empty array. All approaches are very easy to create an array. Manytimes, creating an empty array is most common requirement in your apps. You can create empty array of any type. In this article, you will see different ways to construct an empty array.

Syntax

In Swift, you can create an empty array of a certain type using the following syntax −

var arrayName = [Type]()

Or you can use this alternate syntax −

var arrayName: [Type] = []

Both syntaxes work similarly in Swift.

For example, if you want to create an empty array of integers, you can use

var myArray = [Int]()

This creates an empty array of integers with the variable name myArray.

Here's an example of creating an empty array of strings −

var myArray = [String]()

This creates an empty array of strings with the variable name myArray.

An empty array of booleans −

var myArray = [Bool]()

An empty array of arrays −

var myArray = [[Int]]()

This creates an empty array of arrays of integers with the variable name myArray.

An empty array of tuples −

var myArray = [(String, Int)]()

This creates an empty array of tuples with the variable name myArray. The tuples have one element of type String and one element of type Int.

An empty array of optional −

var myArray = [Int?]()

This creates an empty array of optional integers with the variable name myArray. Each element of the array can either be an integer or nil.

Create an array with a default value by using the init(repeating:count:) initializer, like this −

var myArray = Array(repeating: 0, count: 10)

This creates an array of 10 integers, all initialized to 0.

Here are some important points to know when working with arrays in Swift

  • Arrays in Swift are ordered collections of values of the same type.

  • You can create an empty array by using the syntax var myArray = [Type](), where Type is the type of the elements in the array.

  • You can also use the shorthand syntax [] to create an empty array of inferred type, but this creates an array of type Any, which may not be what you want.

  • You can access elements in an array using their index. The index of the first element in the array is 0, and the index of the last element is array.count - 1.

  • You can add elements to an array using the append(_:) method or the += operator. You can also insert an element at a specific index using the insert(_:at:) method.

  • You can remove elements from an array using the remove(at:) method or the removeLast() method.

  • You can check if an array contains a specific element using the contains(_:) method.

  • You can iterate over the elements in an array using a for-in loop.

  • You can sort the elements in an array using the sort() method, which sorts the elements in ascending order, or the sorted() method, which returns a new array with the elements sorted in ascending order.

  • You can also filter an array to create a new array that contains only the elements that meet a certain condition, using the filter(_:) method.

Conclusion

Arrays are a significant and practical data format in Swift, to sum up. The syntax var myArray = [Type](), where Type denotes the type of the array's components, can be used to make an empty array. An array's components can be accessed, added, and removed from, as well as iterated over, sorted, and filtered according to a set of criteria. To prevent runtime problems, it's crucial to always verify that the index you're reading is present. Additionally, Swift arrays have value semantics, which means that a new duplicate of the array is made when you assign an array to a new variable or send it to a function rather than just a reference to the original array.

Updated on: 04-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements