Swift – Create Array with Initial Values
To create an Array with initial values in Swift, use square brackets. Get all the initial values, separated by comma, in the opening and closing square brackets. This is our Array with initial values. We may assign it to a variable.
Example
In the following program, we create an Int Array with initial values: 2, 4, 6 and 8.
main.swift
</>
Copy
var someEvenNumbers = [2, 4, 6, 8]
print("Array : \(someEvenNumbers)")
Output
Array : [2, 4, 6, 8]
Similarly, we could create Array of any type with required initial values of that type.
Now, let us create an Array with initial values: “United States”, “India”, “Russia”, “China” and “Japan”.
main.swift
</>
Copy
var someCountries = ["USA", "India", "Russia", "China", "Japan"]
print("Array : \(someCountries)")
Output
Array : ["USA", "India", "Russia", "China", "Japan"]
Conclusion
In this Swift Tutorial, we learned how to create an Array with initial values.