Swift: Creating an Empty Array

Creating an Empty Array in Swift:

In Swift, an array is an ordered list of values of the same data type. The syntax to create an empty array of a specific data type in Swift is as follows:

var name = [Type]();

Let’s say we want to make an empty array of type Int called marks. We can create the array by following the syntax above:

var marks = [Int]();

We can now add integers (and only integers) to the marks array after it has been initialized.

Note: Only items of the same data type can be stored in arrays. The array’s data type cannot be changed once it has been declared.

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: