Basics of Arrays

In swift, arrays are useful to store the same data type of multiple values in an ordered manner. In array, we can store the same value in multiple times at different positions based on our requirements.

In swift arrays, we can store any kind of elements like either strings or integers or other element types based on our requirements.

Empty arrays:

var array: [String] = [] // type annotation + array literal
var array = [String]() // invoking the [String] initializer
var array = Array() // without syntactic sugar

An array of string elements:

var names = ["Adam", "Danny", "Pavel"]

Array literals:

let arrayOfInt = [1, 3, 5]

Leave a Reply

Your email address will not be published. Required fields are marked *