Create populated in Swift

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Swift

In Swift, an array can be created using the following syntax:

var arrayName: [dataType] = []

Here is an example to create an array of integers:

var numbers: [Int] = [1, 2, 3, 4, 5]
This creates an array called numbers containing the integers 1 through 5.

You can also create an empty array and add elements to it later using the append() method. Here is an example to create an empty array of strings:

var names: [String] = []

To add elements to the names array, you can use the append() method:

names.append("Alice")
names.append("Bob")
names.append("Charlie")

This would add the strings "Alice", "Bob", and "Charlie" to the names array.